Skip to content

Commit

Permalink
Add help text and -q options
Browse files Browse the repository at this point in the history
  • Loading branch information
klange committed Mar 6, 2024
1 parent 3e66328 commit 5345006
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 10 deletions.
33 changes: 27 additions & 6 deletions tools/callgrind.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,32 @@ extern KrkValue krk_operator_add (KrkValue a, KrkValue b);
extern KrkValue krk_operator_sub (KrkValue a, KrkValue b);

static int usage(char * argv[]) {
fprintf(stderr, "usage: %s [FILE] [args...]\n", argv[0]);
fprintf(stderr, "usage: %s [-f LOG_FILE] [-q] FILE [args...]\n", argv[0]);
return 1;
}

static int help(char * argv[]) {
usage(argv);
fprintf(stderr,
"Generate callgrind-format trace files.\n"
"\n"
"Options:\n"
" -f LOG_FILE Output callgrind-format data to LOG_FILE.\n"
" -q Do not print execution summary.\n"
"\n"
" --help Show this help text.\n"
"\n");
return 0;
}

static KrkValue lineCache; /* {sourceCO: {line: count}} */
static KrkValue callCache; /* {sourceCO: {(codeobject,targetLine,sourceLine): [calls,instructions,time]}} */
static KrkValue timeCache; /* {sourceCO: time} */

static size_t lastFrameCount = 0; /* Previously seen frame count, to track function entry/exit */
static size_t instrCounter = 0; /* Total counter of executed instructions */
static size_t functionsEntered = 0; /* Number of function entries seen */
static int quiet = 0;

/**
* @brief Calculate time difference as string.
Expand Down Expand Up @@ -203,11 +218,14 @@ int main(int argc, char *argv[]) {
snprintf(outfile,1024,"callgrind.out.%d",getpid());

int opt;
while ((opt = getopt(argc, argv, "+:f:-:")) != -1) {
while ((opt = getopt(argc, argv, "+:f:q-:")) != -1) {
switch (opt) {
case 'f':
snprintf(outfile,1024,"%s", optarg);
break;
case 'q':
quiet = 1;
break;
case '?':
if (optopt != '-') {
fprintf(stderr, "%s: unrocognized option '%c'\n", argv[0], optopt);
Expand All @@ -217,6 +235,7 @@ int main(int argc, char *argv[]) {
/* fall through */
case '-':
if (!strcmp(optarg,"help")) {
return help(argv);
} else {
fprintf(stderr, "%s: unrecognized option: '--%s'\n", argv[0], optarg);
return 1;
Expand Down Expand Up @@ -245,14 +264,16 @@ int main(int argc, char *argv[]) {

if (krk_currentThread.flags & KRK_THREAD_HAS_EXCEPTION) {
krk_currentThread.flags &= ~(KRK_THREAD_HAS_EXCEPTION);
fprintf(stderr, "== Executed ended by exception ==\n");
if (!quiet) fprintf(stderr, "== Executed ended by exception ==\n");
} else {
fprintf(stderr, "== Execution completed ==\n");
if (!quiet) fprintf(stderr, "== Execution completed ==\n");
}
krk_callgrind_debuggerHook(NULL);

fprintf(stderr, "%10zu total instruction%s\n", instrCounter, (instrCounter != 1) ? "s" : "");
fprintf(stderr, "%10zu function%s calls\n", functionsEntered, (functionsEntered != 1) ? "s" : "");
if (!quiet) {
fprintf(stderr, "%10zu total instruction%s\n", instrCounter, (instrCounter != 1) ? "s" : "");
fprintf(stderr, "%10zu function%s calls\n", functionsEntered, (functionsEntered != 1) ? "s" : "");
}

FILE * f = fopen(outfile,"w");
if (!f) {
Expand Down
36 changes: 32 additions & 4 deletions tools/watchdog.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,35 @@

#include "common.h"

#define DEFAULT_LIMIT 500000

static int usage(char * argv[]) {
fprintf(stderr, "usage: %s [-s count] [FILE] [args...]\n", argv[0]);
fprintf(stderr, "usage: %s [-s COUNT] [-q] FILE [args...]\n", argv[0]);
return 1;
}

static int help(char * argv[]) {
usage(argv);
fprintf(stderr,
"Run scripts with an instruction counter and halt when a "
"limit is exceeded. The default limit is %d. A total count of "
"executed instructions is printed after completion.\n"
"\n"
"Options:\n"
" -s COUNT Set watchdog timeout to COUNT instructions.\n"
" Specify -1 to set disable limit.\n"
" -q Do not print total instruction count.\n"
"\n"
" --help Show this help text.\n"
"\n",
DEFAULT_LIMIT);
return 0;
}


static size_t instrCounter = 0; /* Total counter of executed instructions */
static size_t stopAt = 500000;
static size_t stopAt = DEFAULT_LIMIT;
static int quiet = 0;

int krk_callgrind_debuggerHook(KrkCallFrame * frame) {
instrCounter++;
Expand All @@ -29,11 +51,14 @@ int krk_callgrind_debuggerHook(KrkCallFrame * frame) {

int main(int argc, char *argv[]) {
int opt;
while ((opt = getopt(argc, argv, "+:s:-:")) != -1) {
while ((opt = getopt(argc, argv, "+:s:q-:")) != -1) {
switch (opt) {
case 's':
stopAt = strtoul(optarg,NULL,10);
break;
case 'q':
quiet = 1;
break;
case '?':
if (optopt != '-') {
fprintf(stderr, "%s: unrocognized option '%c'\n", argv[0], optopt);
Expand All @@ -43,6 +68,7 @@ int main(int argc, char *argv[]) {
/* fall through */
case '-':
if (!strcmp(optarg,"help")) {
return help(argv);
} else {
fprintf(stderr, "%s: unrecognized option: '--%s'\n", argv[0], optarg);
return 1;
Expand All @@ -62,7 +88,9 @@ int main(int argc, char *argv[]) {
krk_startModule("__main__");
krk_runfile(argv[optind],argv[optind]);

fprintf(stderr, "%zu total instructions\n", instrCounter);
if (!quiet) {
fprintf(stderr, "%zu total instructions\n", instrCounter);
}

krk_freeVM();
return 0;
Expand Down

0 comments on commit 5345006

Please sign in to comment.