omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / research / m15 / spbm / reduce.c
| 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | /* Deterministic reduction of the calibration sampler's exact JSONL format. |
| 3 | * This is not the R15 performance reducer and cannot issue an R15 PASS. |
| 4 | */ |
| 5 | #include <inttypes.h> |
| 6 | #include <math.h> |
| 7 | #include <stdint.h> |
| 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <string.h> |
| 11 | |
| 12 | struct sample { unsigned seq; uint64_t begin,end,e[5],p[5],nv_begin,nv_end,nv_e,nv_p; }; |
| 13 | static void fail(const char *s) { fprintf(stderr,"calibration: %s\n",s); exit(1); } |
| 14 | static int parse(const char *s,struct sample *v) |
| 15 | { |
| 16 | int n=0,got=sscanf(s,"{\"sample\":%u,\"read_begin_ns\":%"SCNu64",\"read_end_ns\":%"SCNu64",\"energy_uj\":[%"SCNu64",%"SCNu64",%"SCNu64",%"SCNu64",%"SCNu64"],\"power_uw\":[%"SCNu64",%"SCNu64",%"SCNu64",%"SCNu64",%"SCNu64"],\"overflow_raw\":[0,0,0,0,0],\"nvml_begin_ns\":%"SCNu64",\"nvml_end_ns\":%"SCNu64",\"nvml_energy_mj\":%"SCNu64",\"nvml_power_mw\":%"SCNu64"}%n", |
| 17 | &v->seq,&v->begin,&v->end,&v->e[0],&v->e[1],&v->e[2],&v->e[3],&v->e[4], |
| 18 | &v->p[0],&v->p[1],&v->p[2],&v->p[3],&v->p[4],&v->nv_begin,&v->nv_end,&v->nv_e,&v->nv_p,&n); |
| 19 | return got==17 && n>0 && (s[n]=='\n' || s[n]==0); |
| 20 | } |
| 21 | static double mid(const struct sample *v) { return v->begin+(v->end-v->begin)/2.0; } |
| 22 | static double nv_mid(const struct sample *v) { return v->nv_begin+(v->nv_end-v->nv_begin)/2.0; } |
| 23 | static void reduce(const char *file,const char *phase,int round,int comma) |
| 24 | { |
| 25 | FILE *f=fopen(file,"r"); char line[2048]; |
| 26 | struct sample a={0},prev={0},cur={0}; |
| 27 | double integral[5]={0},nv_integral=0,max_span=0,max_gap=0; |
| 28 | unsigned count=0; |
| 29 | if(!f) fail(file); |
| 30 | while(fgets(line,sizeof(line),f)) { |
| 31 | if(!parse(line,&cur) || cur.seq!=count || cur.end<cur.begin || cur.end-cur.begin>10000000 || cur.nv_begin<cur.end || cur.nv_end<cur.nv_begin) fail("bad sample record"); |
| 32 | if(cur.end-cur.begin>max_span) max_span=cur.end-cur.begin; |
| 33 | for(int i=0;i<5;i++) if(cur.e[i]>UINT32_MAX*UINT64_C(1000) || cur.e[i]%1000 || cur.p[i]>600000000) fail("invalid sensor value"); |
| 34 | if(!count) a=cur; |
| 35 | else { |
| 36 | double dt=(mid(&cur)-mid(&prev))/1e9; |
| 37 | if(cur.begin<=prev.end || dt>.3) fail("nonmonotonic or missing sample"); |
| 38 | if(dt>max_gap) max_gap=dt; |
| 39 | for(int i=0;i<5;i++) { |
| 40 | if(cur.e[i]<prev.e[i]) fail("energy counter decreased"); |
| 41 | integral[i]+=dt*(cur.p[i]+prev.p[i])/2e6; |
| 42 | } |
| 43 | if(cur.nv_e<prev.nv_e || cur.nv_begin<=prev.nv_end) fail("NVML counter/time decreased"); |
| 44 | nv_integral+=(nv_mid(&cur)-nv_mid(&prev))/1e9*(cur.nv_p+prev.nv_p)/2e3; |
| 45 | } |
| 46 | prev=cur;count++; |
| 47 | } |
| 48 | if(ferror(f) || count!=101) fail("missing samples"); |
| 49 | fclose(f); |
| 50 | double dt=(mid(&cur)-mid(&a))/1e9,ndt=(nv_mid(&cur)-nv_mid(&a))/1e9,ew[5]; |
| 51 | for(int i=0;i<5;i++) ew[i]=(cur.e[i]-a.e[i])/dt/1e6; |
| 52 | printf("%s{\"phase\":\"%s\",\"round\":%d,\"samples\":%u,\"seconds\":%.6f,\"energy_mean_w\":[",comma?",\n":"",phase,round,count,dt); |
| 53 | for(int i=0;i<5;i++) printf("%s%.6f",i?",":"",ew[i]); |
| 54 | fputs("],\"integrated_power_mean_w\":[",stdout); |
| 55 | for(int i=0;i<5;i++) printf("%s%.6f",i?",":"",integral[i]/dt); |
| 56 | printf("],\"nvml_energy_mean_w\":%.6f,\"nvml_power_mean_w\":%.6f,\"gpc_unverified\":true,\"max_read_span_us\":%.3f,\"max_sample_gap_ms\":%.3f}", |
| 57 | (cur.nv_e-a.nv_e)/ndt/1e3,nv_integral/ndt,max_span/1e3,max_gap*1e3); |
| 58 | } |
| 59 | int main(int argc,char **argv) |
| 60 | { |
| 61 | /* Phases default to collections 01-03 (idle, p, e); later collections |
| 62 | * name theirs after the directory: reduce DIR [phase...]. */ |
| 63 | const char *def[]={"idle","p","e"};const char **phase=def;int np=3; |
| 64 | char path[1024];int n=0; |
| 65 | if(argc<2) { fprintf(stderr,"usage: reduce run-directory [phase...]\n"); return 2; } |
| 66 | if(argc>2) { phase=(const char **)(argv+2); np=argc-2; } |
| 67 | fputs("{\"schema\":\"AIEN_SPBM_CALIBRATION_OBSERVATIONS_V2\",\"qualified\":false,\"R15_PASS\":false,\"energy_order\":[\"pkg\",\"cpu_e\",\"cpu_p\",\"gpc_unverified\",\"gpm\"],\"power_order\":[\"sys_total\",\"soc_pkg\",\"cpu_e\",\"cpu_p\",\"gpu\"],\"windows\":[\n",stdout); |
| 68 | for(int round=1;round<=3;round++) for(int i=0;i<np;i++) { |
| 69 | if(snprintf(path,sizeof(path),"%s/%s-%d.jsonl",argv[1],phase[i],round)>=(int)sizeof(path)) fail("path too long"); |
| 70 | reduce(path,phase[i],round,n++); |
| 71 | } |
| 72 | fputs("\n]}\n",stdout); |
| 73 | return ferror(stdout)?1:0; |
| 74 | } |