Skip to content

Commit 3cdd3c1

Browse files
committed
feat(query): add batch tables to query db
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 2e0188d commit 3cdd3c1

File tree

2 files changed

+84
-2
lines changed

2 files changed

+84
-2
lines changed

src/main/scala/DPIC.scala

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ class DPICBatch(template: Seq[DifftestBundle], batchIO: BatchIO, config: Gateway
262262
override def desiredName: String = "DiffExtBatch"
263263
override def dpicFuncAssigns: Seq[String] = {
264264
val bundleEnum = template.map(_.desiredModuleName.replace("Difftest", "")) ++ Seq("BatchStep", "BatchFinish")
265+
val batchQueryNumSize = template.length + 1
265266
val bundleAssign = template.zipWithIndex.map { case (t, idx) =>
266267
val bundleName = bundleEnum(idx)
267268
val perfName = "perf_Batch_" + bundleName
@@ -271,6 +272,9 @@ class DPICBatch(template: Seq[DifftestBundle], batchIO: BatchIO, config: Gateway
271272
| dpic_calls[$perfName] += num;
272273
| dpic_bytes[$perfName] += num * ${t.getByteAlignWidth / 8};
273274
|#endif // CONFIG_DIFFTEST_PERFCNT
275+
|#ifdef CONFIG_DIFFTEST_QUERY
276+
| batch_query_nums[$bundleName] += num;
277+
|#endif // CONFIG_DIFFTEST_QUERY
274278
| for (int j = 0; j < num; j++) {
275279
| ${getDPICBundleUnpack(t)}
276280
| }
@@ -311,16 +315,29 @@ class DPICBatch(template: Seq[DifftestBundle], batchIO: BatchIO, config: Gateway
311315
| ${bundleEnum.mkString(",\n ")}
312316
| };
313317
| static int dut_index = 0;
318+
|#ifdef CONFIG_DIFFTEST_QUERY
319+
| static int batch_query_nums[$batchQueryNumSize] = {0};
320+
|#endif // CONFIG_DIFFTEST_QUERY
314321
| $batchDecl
315322
| for (int i = 0; i < $infoLen; i++) {
316323
| if (!diffstate_buffer) return;
317324
| uint8_t id = info[i].id;
318325
| uint8_t num = info[i].num;
319326
| uint32_t coreid, index, address;
327+
|#ifdef CONFIG_DIFFTEST_QUERY
328+
| if (qStats) qStats->BatchInfo_write(id, num);
329+
|#endif // CONFIG_DIFFTEST_QUERY
320330
| if (id == BatchFinish) {
321331
| break;
322332
| }
323333
| else if (id == BatchStep) {
334+
|#ifdef CONFIG_DIFFTEST_QUERY
335+
| batch_query_nums[BatchStep] = num;
336+
| if (qStats) {
337+
| qStats->BatchStep_write(batch_query_nums);
338+
| }
339+
| memset(batch_query_nums, 0, sizeof(batch_query_nums));
340+
|#endif // CONFIG_DIFFTEST_QUERY
324341
| $stepPending
325342
| dut_index = (dut_index + 1) % CONFIG_DIFFTEST_BATCH_SIZE;
326343
|#ifdef CONFIG_DIFFTEST_INTERNAL_STEP
@@ -391,7 +408,7 @@ object DPIC {
391408
}
392409

393410
def batch(template: Seq[DifftestBundle], control: GatewaySinkControl, io: BatchIO, config: GatewayConfig): Unit = {
394-
Query.register(template, "", "0")
411+
Query.registerBatch(template, "", "0")
395412
val module = Module(new DummyDPICBatchWrapper(template, chiselTypeOf(io), config))
396413
module.control := control
397414
module.io := io

src/main/scala/util/Query.scala

Lines changed: 66 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,62 @@ 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 templateNames: Seq[String] = template.map(_.desiredModuleName.replace("Difftest", ""))
133+
private val bundleNames: Seq[String] = templateNames ++ Seq("BatchStep", "BatchFinish")
134+
private val stepColNames: Seq[String] = templateNames ++ Seq("BatchStep")
135+
136+
val members: String =
137+
s"""Query* query_BatchInfo;
138+
| Query* query_BatchStep;""".stripMargin
139+
140+
val initInvoke: String = "BatchTable_init();"
141+
142+
val initDecl: String = {
143+
val bundleNameInserts = bundleNames.zipWithIndex.map { case (name, id) =>
144+
s"INSERT INTO BundleNames VALUES($id, '$name');"
145+
}.mkString("")
146+
147+
s"""
148+
| void BatchTable_init() {
149+
| const char* createBatchInfoSql = "CREATE TABLE BatchInfo("
150+
| "ID INTEGER PRIMARY KEY AUTOINCREMENT,"
151+
| "STEP INTEGER NOT NULL,"
152+
| "BUNDLE_ID INTEGER NOT NULL,"
153+
| "NUM INTEGER NOT NULL);";
154+
| const char* insertBatchInfoSql = "INSERT INTO BatchInfo (STEP,BUNDLE_ID,NUM)"
155+
| " VALUES (?,?,?);";
156+
| query_BatchInfo = new Query(mem_db, createBatchInfoSql, insertBatchInfoSql);
157+
|
158+
| const char* createBundleNamesSql = "CREATE TABLE BundleNames("
159+
| "BUNDLE_ID INTEGER PRIMARY KEY,"
160+
| "NAME TEXT NOT NULL);";
161+
| char* errMsg;
162+
| sqlite3_exec(mem_db, createBundleNamesSql, 0, 0, &errMsg);
163+
| sqlite3_exec(mem_db, "$bundleNameInserts", 0, 0, &errMsg);
164+
|
165+
| const char* createBatchStepSql = "CREATE TABLE BatchStep("
166+
| "ID INTEGER PRIMARY KEY AUTOINCREMENT,"
167+
| "STEP INTEGER NOT NULL,"
168+
| "${stepColNames.map(n => s"$n INTEGER NOT NULL").mkString(",")});";
169+
| const char* insertBatchStepSql = "INSERT INTO BatchStep (STEP,${stepColNames.mkString(",")}) "
170+
| "VALUES (${Seq.fill(stepColNames.length + 1)("?").mkString(",")});";
171+
| query_BatchStep = new Query(mem_db, createBatchStepSql, insertBatchStepSql);
172+
| }
173+
|""".stripMargin
174+
}
175+
176+
val writeDecl: String = {
177+
val numArgs = stepColNames.indices.map(i => s"nums[$i]").mkString(", ")
178+
val writeArgCount = stepColNames.length + 1 // STEP + stepColNames
179+
s"""
180+
| void BatchInfo_write(int bundle_id, int num) {
181+
| query_BatchInfo->write(3, (int)query_step, bundle_id, num);
182+
| }
183+
| void BatchStep_write(int* nums) {
184+
| query_BatchStep->write($writeArgCount, (int)query_step, $numArgs);
185+
| }
186+
|""".stripMargin
187+
}
188+
}

0 commit comments

Comments
 (0)