#include "sysCallStat.h" #ifdef MEASURE_SYS_CALLS extern KRNL_API_STATS API_STATS[sysCallMax]; /* "enc", "-pbkdf2 -aes-256-cbc -in plain.txt -out cipher.enc -kfile keyfile.key" */ void clearSysTableData() { int i=0; for (; i< sysCallMax; i++) { API_STATS[i].execTimeUsec=0; API_STATS[i].sysCallCount=0; } } void sysCallMeasurementTask(const char* cmd, const char* arg) { sysCallMsrSetData (1, sysCallTaskID); shellCmdExec (cmd, arg); semBGive(sysCallSemID); sysCallMsrSetData (0, 0); displayStats(); } void CaptureSysCallMetrics(const char* cmd, const char* arg) { tsFreq = sysTimestampFreq(); /*Set this once during init and neednot set during every system call execution.*/ sysCallSemID = semBCreate(SEM_Q_FIFO,SEM_EMPTY); if(!sysCallSemID) { printf("CaptureSysCallMetrics: Failed to create semaphore\n"); return ; } clearSysTableData(); sysCallTaskID= taskSpawn("tSysCallMeasure", 50, /* priority */ 0, /* options */ 8192, /* stack */ (FUNCPTR)sysCallMeasurementTask, cmd, arg,0,0,0,0,0,0,0,0); if(sysCallTaskID == ERROR) /*This is to avoid parent task in block state if taskSpawn fails to create a task.*/ { printf("CaptureSysCallMetrics: Failed to create a task\n"); return ; } semBTake(sysCallSemID, WAIT_FOREVER); /*As we crated semaphore with SEM_EMPTY, so it is unavilable and gets blocked here for parent and gets unblcoked when child executes semGive().*/ semDelete(sysCallSemID); } void displayStats() { UINT64 openTimeUsec = 0; UINT64 closeTimeUsec = 0; UINT64 readTimeUsec = 0; UINT64 writeTimeUsec = 0; if (tsFreq == 0) { printf("ERROR: tsFreq is zero\n"); return; } openTimeUsec = (API_STATS[openCall].execTimeUsec * 1000000ULL) / (UINT64)tsFreq; closeTimeUsec = (API_STATS[closeCall].execTimeUsec * 1000000ULL) / (UINT64)tsFreq; readTimeUsec = (API_STATS[readCall].execTimeUsec * 1000000ULL) / (UINT64)tsFreq; writeTimeUsec = (API_STATS[writeCall].execTimeUsec * 1000000ULL) / (UINT64)tsFreq; printf("------------------------------------------------------------------\n"); printf("%-15s %-15s %-15s %-20s\n","crypto cmd","system call","Invocations","Total time (USec)"); printf("------------------------------------------------------------------\n"); printf("%-15s %-15s %-15u %-20llu\n","enc","open",API_STATS[openCall].sysCallCount,openTimeUsec); printf("%-15s %-15s %-15u %-20llu\n","enc","close",API_STATS[closeCall].sysCallCount,closeTimeUsec); printf("%-15s %-15s %-15u %-20llu\n","enc","read",API_STATS[readCall].sysCallCount,readTimeUsec); printf("%-15s %-15s %-15u %-20llu\n","enc","write",API_STATS[writeCall].sysCallCount,writeTimeUsec); printf("------------------------------------------------------------------\n"); } #endif