Skip to content

Commit 64a81d5

Browse files
authored
feat(query): add batch tables to query db (#846)
Add BatchInfo and BatchStep tables to persist batch entry records and per-step bundle count summaries in the query database. Also add the BundleNames table to provide a readable mapping from bundle ids to bundle names.
1 parent 3493357 commit 64a81d5

File tree

2 files changed

+79
-2
lines changed

2 files changed

+79
-2
lines changed

src/main/scala/DPIC.scala

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,16 +311,29 @@ class DPICBatch(template: Seq[DifftestBundle], batchIO: BatchIO, config: Gateway
311311
| ${bundleEnum.mkString(",\n ")}
312312
| };
313313
| static int dut_index = 0;
314+
|#ifdef CONFIG_DIFFTEST_QUERY
315+
| static int batch_query_nums[${bundleEnum.length}] = {0};
316+
|#endif // CONFIG_DIFFTEST_QUERY
314317
| $batchDecl
315318
| for (int i = 0; i < $infoLen; i++) {
316319
| if (!diffstate_buffer) return;
317320
| uint8_t id = info[i].id;
318321
| uint8_t num = info[i].num;
319322
| uint32_t coreid, index, address;
323+
|#ifdef CONFIG_DIFFTEST_QUERY
324+
| if (qStats) {
325+
| qStats->BatchInfo_write(id, num);
326+
| batch_query_nums[id] = num;
327+
| }
328+
|#endif // CONFIG_DIFFTEST_QUERY
320329
| if (id == BatchFinish) {
321330
| break;
322331
| }
323332
| else if (id == BatchStep) {
333+
|#ifdef CONFIG_DIFFTEST_QUERY
334+
| if (qStats) qStats->BatchStep_write(batch_query_nums);
335+
| memset(batch_query_nums, 0, sizeof(batch_query_nums));
336+
|#endif // CONFIG_DIFFTEST_QUERY
324337
| $stepPending
325338
| dut_index = (dut_index + 1) % CONFIG_DIFFTEST_BATCH_SIZE;
326339
|#ifdef CONFIG_DIFFTEST_INTERNAL_STEP
@@ -391,7 +404,7 @@ object DPIC {
391404
}
392405

393406
def batch(template: Seq[DifftestBundle], control: GatewaySinkControl, io: BatchIO, config: GatewayConfig): Unit = {
394-
Query.register(template, "", "0")
407+
Query.registerBatch(template, "", "0")
395408
val module = Module(new DummyDPICBatchWrapper(template, chiselTypeOf(io), config))
396409
module.control := control
397410
module.io := io

src/main/scala/util/Query.scala

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@ import scala.collection.mutable.ListBuffer
2323

2424
object Query {
2525
private val tables = ListBuffer.empty[QueryTable]
26+
private var batchTable: Option[BatchQueryTable] = None
2627
def register(gen: DifftestBundle, locPrefix: String, dutZone: String) = {
2728
tables += new QueryTable(gen, locPrefix, dutZone)
2829
}
29-
def register(gens: Seq[DifftestBundle], locPrefix: String, dutZone: String) = {
30+
def registerBatch(gens: Seq[DifftestBundle], locPrefix: String, dutZone: String) = {
3031
gens.foreach { gen => tables += new QueryTable(gen, locPrefix, dutZone) }
32+
batchTable = Some(new BatchQueryTable(gens))
3133
}
3234
def writeInvoke(gen: DifftestBundle): String = {
3335
tables.find(_.gen == gen).get.writeInvoke
@@ -51,11 +53,15 @@ object Query {
5153
|class QueryStats: public QueryStatsBase {
5254
|public:
5355
| ${tables.map { t => s"Query* ${t.instName};" }.mkString("\n ")}
56+
| ${batchTable.map(_.members).getOrElse("")}
5457
| QueryStats(char *path): QueryStatsBase(path) {
5558
| ${tables.map(_.initInvoke).mkString("\n ")}
59+
| ${batchTable.map(_.initInvoke).getOrElse("")}
5660
| }
5761
| ${tables.map(_.initDecl).mkString("")}
5862
| ${tables.map(_.writeDecl).mkString("")}
63+
| ${batchTable.map(_.initDecl).getOrElse("")}
64+
| ${batchTable.map(_.writeDecl).getOrElse("")}
5965
|};
6066
|#endif // CONFIG_DIFFTEST_QUERY
6167
|#endif // __DIFFTEST_QUERY_H__
@@ -121,3 +127,61 @@ class QueryTable(val gen: DifftestBundle, locPrefix: String, dutZone: String) {
121127
}
122128
val writeInvoke = s"qStats->${tableName}_write($dutZone, ${locArgs.map(locPrefix + _._2).mkString(", ")}, packet);"
123129
}
130+
131+
class BatchQueryTable(template: Seq[DifftestBundle]) {
132+
private val bundleNames: Seq[String] =
133+
template.map(_.desiredModuleName.replace("Difftest", "")) ++ Seq("BatchStep", "BatchFinish")
134+
135+
val members: String =
136+
s"""Query* query_BatchInfo;
137+
| Query* query_BatchStep;""".stripMargin
138+
139+
val initInvoke: String = "BatchTable_init();"
140+
141+
val initDecl: String = {
142+
val bundleNameInserts = bundleNames.zipWithIndex.map { case (name, id) =>
143+
s"INSERT INTO BundleNames VALUES($id, '$name');"
144+
}.mkString("")
145+
146+
s"""
147+
| void BatchTable_init() {
148+
| const char* createBatchInfoSql = "CREATE TABLE BatchInfo("
149+
| "ID INTEGER PRIMARY KEY AUTOINCREMENT,"
150+
| "STEP INTEGER NOT NULL,"
151+
| "BUNDLE_ID INTEGER NOT NULL,"
152+
| "NUM INTEGER NOT NULL);";
153+
| const char* insertBatchInfoSql = "INSERT INTO BatchInfo (STEP,BUNDLE_ID,NUM)"
154+
| " VALUES (?,?,?);";
155+
| query_BatchInfo = new Query(mem_db, createBatchInfoSql, insertBatchInfoSql);
156+
|
157+
| const char* createBundleNamesSql = "CREATE TABLE BundleNames("
158+
| "BUNDLE_ID INTEGER PRIMARY KEY,"
159+
| "NAME TEXT NOT NULL);";
160+
| char* errMsg;
161+
| sqlite3_exec(mem_db, createBundleNamesSql, 0, 0, &errMsg);
162+
| sqlite3_exec(mem_db, "$bundleNameInserts", 0, 0, &errMsg);
163+
|
164+
| const char* createBatchStepSql = "CREATE TABLE BatchStep("
165+
| "ID INTEGER PRIMARY KEY AUTOINCREMENT,"
166+
| "STEP INTEGER NOT NULL,"
167+
| "${bundleNames.map(n => s"$n INTEGER NOT NULL").mkString(",")});";
168+
| const char* insertBatchStepSql = "INSERT INTO BatchStep (STEP,${bundleNames.mkString(",")}) "
169+
| "VALUES (${Seq.fill(bundleNames.length + 1)("?").mkString(",")});";
170+
| query_BatchStep = new Query(mem_db, createBatchStepSql, insertBatchStepSql);
171+
| }
172+
|""".stripMargin
173+
}
174+
175+
val writeDecl: String = {
176+
val numArgs = bundleNames.indices.map(i => s"nums[$i]").mkString(", ")
177+
val writeArgCount = bundleNames.length + 1 // STEP + bundleNames
178+
s"""
179+
| void BatchInfo_write(int bundle_id, int num) {
180+
| query_BatchInfo->write(3, (int)query_step, bundle_id, num);
181+
| }
182+
| void BatchStep_write(int* nums) {
183+
| query_BatchStep->write($writeArgCount, (int)query_step, $numArgs);
184+
| }
185+
|""".stripMargin
186+
}
187+
}

0 commit comments

Comments
 (0)