Skip to content

Commit cf5c213

Browse files
committed
Add cmdline option to stop the emulator after a number of cycles
For performance testing
1 parent bf75987 commit cf5c213

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,14 @@ You can exit the emulator using: \<Ctrl-a x\>. (press Ctrl+A, leave it, afterwar
6969
## Usage
7070

7171
```shell
72-
./semu -k linux-image [-b dtb-file] [-i initrd-image] [-d disk-image]
72+
./semu -k linux-image [-b dtb-file] [-i initrd-image] [-d disk-image] [-c max_cycles]
7373
```
7474

7575
* `linux-image` is the path to the Linux kernel `Image`.
7676
* `dtb-file` is optional, as it specifies the user-specified device tree blob.
7777
* `initrd-image` is optional, as it specifies the user-specified initial RAM disk image.
7878
* `disk-image` is optional, as it specifies the path of a disk image in ext4 file system for the virtio-blk device.
79+
* `max_cycles` allows to set a limit of cycles the emulator should run for - for testing performance
7980

8081
## Build Linux kernel image and root file system
8182

main.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,8 @@ static void usage(const char *execpath)
293293
{
294294
fprintf(
295295
stderr,
296-
"Usage: %s -k linux-image [-b dtb] [-i initrd-image] [-d disk-image]\n",
296+
"Usage: %s -k linux-image [-b dtb] [-i initrd-image] "
297+
"[-d disk-image] [-c max_cycles]\n",
297298
execpath);
298299
}
299300

@@ -302,19 +303,21 @@ static void handle_options(int argc,
302303
char **kernel_file,
303304
char **dtb_file,
304305
char **initrd_file,
305-
char **disk_file)
306+
char **disk_file,
307+
uint32_t *cycle_limit)
306308
{
307309
*kernel_file = *dtb_file = *initrd_file = *disk_file = NULL;
308310

309311
int optidx = 0;
310312
struct option opts[] = {
311313
{"kernel", 1, NULL, 'k'}, {"dtb", 1, NULL, 'b'},
312314
{"initrd", 1, NULL, 'i'}, {"disk", 1, NULL, 'd'},
315+
{"max_cycles", 1, NULL, 'c'},
313316
{"help", 0, NULL, 'h'},
314317
};
315318

316319
int c;
317-
while ((c = getopt_long(argc, argv, "k:b:i:d:h", opts, &optidx)) != -1) {
320+
while ((c = getopt_long(argc, argv, "k:b:i:d:c:h", opts, &optidx)) != -1) {
318321
switch (c) {
319322
case 'k':
320323
*kernel_file = optarg;
@@ -328,6 +331,14 @@ static void handle_options(int argc,
328331
case 'd':
329332
*disk_file = optarg;
330333
break;
334+
case 'c':
335+
if (sscanf(optarg, "%u", cycle_limit) != 1) {
336+
fprintf(stderr, "Cannot parse -c max_cycles argument '%s'.\n",
337+
optarg);
338+
usage(argv[0]);
339+
exit(2);
340+
}
341+
break;
331342
case 'h':
332343
usage(argv[0]);
333344
exit(0);
@@ -354,8 +365,9 @@ static int semu_start(int argc, char **argv)
354365
char *dtb_file;
355366
char *initrd_file;
356367
char *disk_file;
368+
uint32_t cycle_limit = 0;
357369
handle_options(argc, argv, &kernel_file, &dtb_file, &initrd_file,
358-
&disk_file);
370+
&disk_file, &cycle_limit);
359371

360372
/* Initialize the emulator */
361373
emu_state_t emu;
@@ -442,6 +454,7 @@ static int semu_start(int argc, char **argv)
442454
if (emu.vblk.InterruptStatus)
443455
emu_update_vblk_interrupts(&vm);
444456
#endif
457+
if (cycle_limit && vm.insn_count >= cycle_limit) exit(0);
445458
}
446459

447460
if (vm.insn_count_hi > emu.timer_hi ||

0 commit comments

Comments
 (0)