--- speed.c 2005-11-02 19:15:44.022323640 +0200 +++ speed.c 2005-11-02 19:15:13.751925440 +0200 @@ -260,10 +260,88 @@ #define START 0 #define STOP 1 +#ifdef __linux__ +/* + * record CPU usage as well + */ + +static int do_cpu = 0; + +struct cpu_stat { + unsigned int user; + unsigned int nice; + unsigned int system; + unsigned int idle; + unsigned int total; +}; + +static unsigned int cpu_usage[ALGOR_NUM][SIZE_NUM]; +static unsigned int rsa_cpu_usage[RSA_NUM][2]; +static unsigned int dsa_cpu_usage[DSA_NUM][2]; +static struct cpu_stat cpu_start, cpu_finish; + +static void +get_cpu(int s) +{ + FILE *fp = NULL; + unsigned char buf[80]; + struct cpu_stat *st = s == START ? &cpu_start : &cpu_finish; + + memset(st, 0, sizeof(*st)); + + if (fp == NULL) + fp = fopen("/proc/stat", "r"); + if (!fp) + return; + if (fseek(fp, 0, SEEK_SET) == -1) { + fclose(fp); + return; + } + fscanf(fp, "%s %d %d %d %d", &buf[0], &st->user, &st->nice, + &st->system, &st->idle); + st->total = st->user + st->nice + st->system + st->idle; + fclose(fp); +} + +static unsigned int +calc_cpu() +{ + unsigned int total, res; + + total = cpu_finish.total - cpu_start.total; + if (total <= 0) + return 0; +#if 1 // busy + res = ((cpu_finish.system + cpu_finish.user + cpu_finish.nice) - + (cpu_start.system + cpu_start.user + cpu_start.nice)) * + 100 / total; +#endif +#if 0 // system + res = (cpu_finish.system - cpu_start.system) * 100 / total; +#endif +#if 0 // user + res = (cpu_finish.user - cpu_start.user) * 100 / total; +#endif +#if 0 // nice + res = (cpu_finish.nice - cpu_start.nice) * 100 / total; +#endif +#if 0 // idle + res = (cpu_finish.idle - cpu_start.idle) * 100 / total; +#endif + return(res); +} + +#endif + static double Time_F(int s) { double ret; +#ifdef __linux__ + if (do_cpu) + get_cpu(s); +#endif + #ifdef USE_TOD if(usertime) { @@ -567,6 +645,14 @@ j--; /* Otherwise, -elapsed gets confused with an algorithm. */ } +#ifdef __linux__ + else if ((argc > 0) && (strcmp(*argv,"-cpu") == 0)) + { + do_cpu = 1; + j--; /* Otherwise, -cpu gets confused with + an algorithm. */ + } +#endif else if ((argc > 0) && (strcmp(*argv,"-evp") == 0)) { argc--; @@ -881,6 +967,9 @@ #ifdef HAVE_FORK BIO_printf(bio_err,"-multi n run n benchmarks in parallel.\n"); #endif +#ifdef __linux__ + BIO_printf(bio_err,"-cpu calculate cpu utilisation.\n"); +#endif goto end; } argc--; @@ -888,11 +977,6 @@ j++; } -#ifdef HAVE_FORK - if(multi && do_multi(multi)) - goto show_res; -#endif - if (j == 0) { for (i=0; inid); if(mr) fprintf(stdout,"+F:%d:%s",k,names[k]); else @@ -1688,6 +1791,8 @@ fprintf(stdout," %11.2fk",results[k][j]/1e3); else fprintf(stdout,mr ? ":%.2f" : " %11.2f ",results[k][j]); + if (do_cpu) + fprintf(stdout, mr ? "/%d" : "/%%%-3d", cpu_usage[k][j]); } fprintf(stdout,"\n"); } @@ -1702,13 +1807,18 @@ j=0; } if(mr) - fprintf(stdout,"+F2:%u:%u:%f:%f\n", - k,rsa_bits[k],rsa_results[k][0], - rsa_results[k][1]); - else - fprintf(stdout,"rsa %4u bits %8.4fs %8.4fs %8.1f %8.1f\n", - rsa_bits[k],rsa_results[k][0],rsa_results[k][1], - 1.0/rsa_results[k][0],1.0/rsa_results[k][1]); + fprintf(stdout,"+F2:%u:%u:%f", k,rsa_bits[k],rsa_results[k][0]); + else + fprintf(stdout,"rsa %4u bits %8.4fs",rsa_bits[k],rsa_results[k][0]); + if (do_cpu) + fprintf(stdout, mr ? "/%d": "/%%%-3d", rsa_cpu_usage[k][0]); + fprintf(stdout, mr ? ":%f" : " %8.4fs", rsa_results[k][1]); + if (do_cpu) + fprintf(stdout, mr ? "/%d": "/%%%-3d", rsa_cpu_usage[k][1]); + if(!mr) + fprintf(stdout, " %8.1f %8.1f", + 1.0/rsa_results[k][0],1.0/rsa_results[k][1]); + fprintf(stdout, "\n"); } #endif #ifndef OPENSSL_NO_DSA @@ -1722,12 +1832,18 @@ j=0; } if(mr) - fprintf(stdout,"+F3:%u:%u:%f:%f\n", - k,dsa_bits[k],dsa_results[k][0],dsa_results[k][1]); + fprintf(stdout,"+F3:%u:%u:%f", k,dsa_bits[k],dsa_results[k][0]); else - fprintf(stdout,"dsa %4u bits %8.4fs %8.4fs %8.1f %8.1f\n", - dsa_bits[k],dsa_results[k][0],dsa_results[k][1], - 1.0/dsa_results[k][0],1.0/dsa_results[k][1]); + fprintf(stdout,"dsa %4u bits %8.4fs",dsa_bits[k],dsa_results[k][0]); + if (do_cpu) + fprintf(stdout, mr ? "/%d": "/%%%-3d", dsa_cpu_usage[k][0]); + fprintf(stdout, mr ? ":%f" : " %8.4fs", dsa_results[k][1]); + if (do_cpu) + fprintf(stdout, mr ? "/%d": "/%%%-3d", dsa_cpu_usage[k][1]); + if(!mr) + fprintf(stdout, " %8.1f %8.1f", + 1.0/dsa_results[k][0],1.0/dsa_results[k][1]); + fprintf(stdout, "\n"); } #endif mret=0; @@ -1786,6 +1902,8 @@ static void print_result(int alg,int run_no,int count,double time_used) { + if (do_cpu) + cpu_usage[alg][run_no] = calc_cpu(); BIO_printf(bio_err,mr ? "+R:%ld:%s:%f\n" : "%ld %s's in %.2fs\n",count,names[alg],time_used); results[alg][run_no]=((double)count)/time_used*lengths[run_no]; @@ -1880,29 +1998,11 @@ p=buf+3; alg=atoi(sstrsep(&p,sep)); sstrsep(&p,sep); - for(j=0 ; j < SIZE_NUM ; ++j) + for(j=0 ; j < SIZE_NUM ; ++j) { + if (do_cpu && strchr(p, '/')) + cpu_usage[alg][j] = atoi(strchr(p, '/') + 1); results[alg][j]+=atof(sstrsep(&p,sep)); } - else if(!strncmp(buf,"+F2:",4)) - { - int k; - double d; - - p=buf+4; - k=atoi(sstrsep(&p,sep)); - sstrsep(&p,sep); - - d=atof(sstrsep(&p,sep)); - if(n) - rsa_results[k][0]=1/(1/rsa_results[k][0]+1/d); - else - rsa_results[k][0]=d; - - d=atof(sstrsep(&p,sep)); - if(n) - rsa_results[k][1]=1/(1/rsa_results[k][1]+1/d); - else - rsa_results[k][1]=d; } else if(!strncmp(buf,"+F2:",4)) { @@ -1913,12 +2013,18 @@ k=atoi(sstrsep(&p,sep)); sstrsep(&p,sep); + /* before we move the token along */ + if (do_cpu && strchr(p, '/')) + rsa_cpu_usage[k][0] = atoi(strchr(p, '/') + 1); d=atof(sstrsep(&p,sep)); if(n) rsa_results[k][0]=1/(1/rsa_results[k][0]+1/d); else rsa_results[k][0]=d; + /* before we move the token along */ + if (do_cpu && strchr(p, '/')) + rsa_cpu_usage[k][1] = atoi(strchr(p, '/') + 1); d=atof(sstrsep(&p,sep)); if(n) rsa_results[k][1]=1/(1/rsa_results[k][1]+1/d); @@ -1934,12 +2040,18 @@ k=atoi(sstrsep(&p,sep)); sstrsep(&p,sep); + /* before we move the token along */ + if (do_cpu && strchr(p, '/')) + dsa_cpu_usage[k][0] = atoi(strchr(p, '/') + 1); d=atof(sstrsep(&p,sep)); if(n) dsa_results[k][0]=1/(1/dsa_results[k][0]+1/d); else dsa_results[k][0]=d; + /* before we move the token along */ + if (do_cpu && strchr(p, '/')) + dsa_cpu_usage[k][1] = atoi(strchr(p, '/') + 1); d=atof(sstrsep(&p,sep)); if(n) dsa_results[k][1]=1/(1/dsa_results[k][1]+1/d);