|
| 1 | +package cfig.lazybox |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.ObjectMapper |
| 4 | +import org.slf4j.LoggerFactory |
| 5 | +import java.io.File |
| 6 | +import java.io.FileNotFoundException |
| 7 | + |
| 8 | +data class DmaBufInfo( |
| 9 | + val size: Long, |
| 10 | + val flags: String, |
| 11 | + val mode: String, |
| 12 | + val count: Int, |
| 13 | + val exp_name: String, |
| 14 | + val ino: String, |
| 15 | + val pid: Int?, |
| 16 | + val tids: List<Int>, |
| 17 | + val processName: String?, |
| 18 | + val attachedDevices: List<String> |
| 19 | +) |
| 20 | + |
| 21 | +class DmaInfoParser { |
| 22 | + |
| 23 | + companion object { |
| 24 | + private val log = LoggerFactory.getLogger(DmaInfoParser::class.java) |
| 25 | + } |
| 26 | + |
| 27 | + fun parse(args: Array<String>) { |
| 28 | + if (args.isEmpty()) { |
| 29 | + log.error("Usage: Provide the path to the dmainfo file as an argument.") |
| 30 | + return |
| 31 | + } |
| 32 | + |
| 33 | + val filePath = args[0] |
| 34 | + log.info("Parsing file: {}", filePath) |
| 35 | + |
| 36 | + try { |
| 37 | + val dmaInfoList = parseFile(filePath) |
| 38 | + |
| 39 | + if (dmaInfoList.isNotEmpty()) { |
| 40 | + val mapper = ObjectMapper() |
| 41 | + val writer = mapper.writerWithDefaultPrettyPrinter() |
| 42 | + dmaInfoList.forEach { info -> |
| 43 | + log.info("Parsed object:\n{}", writer.writeValueAsString(info)) |
| 44 | + } |
| 45 | + log.info("--------------------------------------------------") |
| 46 | + log.info("Successfully parsed {} DMA buffer objects.", dmaInfoList.size) |
| 47 | + } else { |
| 48 | + log.warn("No valid DMA buffer objects were found in the file.") |
| 49 | + } |
| 50 | + } catch (e: FileNotFoundException) { |
| 51 | + log.error("File operation failed: {}", e.message) |
| 52 | + } catch (e: Exception) { |
| 53 | + log.error("An unexpected error occurred during parsing.", e) |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Reads and parses a dmainfo file from the given path. |
| 59 | + * |
| 60 | + * @param filePath The path to the dmainfo file. |
| 61 | + * @return A list of [DmaBufInfo] objects, one for each entry in the file. |
| 62 | + * @throws FileNotFoundException if the file does not exist. |
| 63 | + */ |
| 64 | + private fun parseFile(filePath: String): List<DmaBufInfo> { |
| 65 | + val file = File(filePath) |
| 66 | + if (!file.exists()) { |
| 67 | + throw FileNotFoundException("Error: File not found at '$filePath'") |
| 68 | + } |
| 69 | + |
| 70 | + val allLines = file.readLines() |
| 71 | + |
| 72 | + val firstDataLineIndex = allLines.indexOfFirst { line -> |
| 73 | + line.trim().matches(Regex("""^[0-9a-fA-F]{8}\s+.*""")) |
| 74 | + } |
| 75 | + |
| 76 | + if (firstDataLineIndex == -1) { |
| 77 | + log.warn("No data lines found in the file.") |
| 78 | + return emptyList() |
| 79 | + } |
| 80 | + |
| 81 | + val content = allLines.subList(firstDataLineIndex, allLines.size).joinToString("\n") |
| 82 | + |
| 83 | + val blocks = content.split(Regex("(\\r?\\n){2,}")) |
| 84 | + .map { it.trim() } |
| 85 | + .filter { it.isNotEmpty() } |
| 86 | + |
| 87 | + return blocks.mapNotNull { parseBlock(it) } |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Parses a single block of text representing one DMA buffer object. |
| 92 | + */ |
| 93 | + private fun parseBlock(block: String): DmaBufInfo? { |
| 94 | + val lines = block.lines().filter { it.isNotBlank() } |
| 95 | + if (lines.isEmpty()) return null |
| 96 | + |
| 97 | + val mainLine = lines.first() |
| 98 | + val mainLineRegex = Regex("""^(\w+)\s+(\w+)\s+(\w+)\s+(\d+)\s+([\w-]+)\s+(\w+)\s*(.*)$""") |
| 99 | + val match = mainLineRegex.find(mainLine) |
| 100 | + if (match == null) { |
| 101 | + log.warn("Skipping malformed line that doesn't match expected format: \"{}\"", mainLine) |
| 102 | + return null |
| 103 | + } |
| 104 | + |
| 105 | + val (sizeStr, flagsStr, modeStr, countStr, expName, ino, processStr) = match.destructured |
| 106 | + |
| 107 | + var pid: Int? = null |
| 108 | + val tids = mutableListOf<Int>() |
| 109 | + var processName: String? = null |
| 110 | + |
| 111 | + if (processStr.isNotBlank()) { |
| 112 | + val processParts = processStr.trim().split(Regex("\\s+")) |
| 113 | + val nameParts = mutableListOf<String>() |
| 114 | + var pidFound = false |
| 115 | + |
| 116 | + processParts.forEach { part -> |
| 117 | + val num = part.toIntOrNull() |
| 118 | + if (num != null) { |
| 119 | + if (!pidFound) { |
| 120 | + pid = num |
| 121 | + pidFound = true |
| 122 | + } else { |
| 123 | + tids.add(num) |
| 124 | + } |
| 125 | + } else { |
| 126 | + nameParts.add(part) |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + if (nameParts.isNotEmpty()) { |
| 131 | + processName = nameParts.joinToString(" ") |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + val attachedDevices = lines.drop(1) |
| 136 | + .dropWhile { !it.trim().equals("Attached Devices:", ignoreCase = true) } |
| 137 | + .drop(1) |
| 138 | + .map { it.trim() } |
| 139 | + .takeWhile { !it.trim().startsWith("Total", ignoreCase = true) } |
| 140 | + .filter { it.isNotEmpty() } |
| 141 | + |
| 142 | + return DmaBufInfo( |
| 143 | + size = sizeStr.toLongOrNull() ?: 0L, |
| 144 | + flags = "0x$flagsStr", |
| 145 | + mode = "0x$modeStr", |
| 146 | + count = countStr.toIntOrNull() ?: 0, |
| 147 | + exp_name = expName, |
| 148 | + ino = ino, |
| 149 | + pid = pid, |
| 150 | + tids = tids, |
| 151 | + processName = processName, |
| 152 | + attachedDevices = attachedDevices |
| 153 | + ) |
| 154 | + } |
| 155 | +} |
0 commit comments