Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/research/m15/spbm/sample.c # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / research / m15 / spbm / sample.c` 116 lines ยท 4888 bytes Raw Blame History 1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* Calibration observations only: no MMIO access and no hardware control. */ 3 #define _POSIX_C_SOURCE 200809L 4 #include 5 #include 6 #include 7 #include 8 #include 9 #include 10 #include 11 #include 12 #include 13 #include 14 15 static uint64_t now_ns(void) 16 { 17 struct timespec t; 18 if (clock_gettime(CLOCK_MONOTONIC, &t)) { perror("clock_gettime"); exit(1); } 19 return (uint64_t)t.tv_sec * `1000000000` + t.tv_nsec; 20 } 21 static uint64_t read_value(const char *dir, const char *kind, int n, const char *suffix) 22 { 23 char path[512], buf[80], *end; 24 int fd; 25 ssize_t got; 26 unsigned long long value; 27 if (snprintf(path, sizeof(path), "%s/%s%d_%s", dir, kind, n, suffix) >= (int)sizeof(path)) exit(1); 28 fd = open(path, O_RDONLY | O_CLOEXEC); 29 if (fd < 0) { perror(path); exit(1); } 30 got = read(fd, buf, sizeof(buf)-1); 31 if (got <= 0) { perror(path); close(fd); exit(1); } 32 close(fd); 33 buf[got] = 0; errno = 0; 34 value = strtoull(buf, &end, 10); 35 if (errno || end == buf || (*end != '\n' && *end != 0) || buf[0] == '-') exit(1); 36 return value; 37 } 38 static void check_label(const char *dir, const char *kind, int n, const char *expect) 39 { 40 char path[512], buf[80]; 41 FILE *f; 42 if (snprintf(path,sizeof(path),"%s/%s%d_label",dir,kind,n)>=(int)sizeof(path)) exit(1); 43 f=fopen(path,"r"); 44 if (!f || !fgets(buf,sizeof(buf),f)) { perror(path); exit(1); } 45 fclose(f); buf[strcspn(buf,"\n")]=0; 46 if (strcmp(buf,expect)) { fprintf(stderr,"wrong sensor label: %s\n",path); exit(1); } 47 } 48 49 int main(int argc, char **argv) 50 { 51 const char *el[] = {"pkg","cpu_e","cpu_p","gpc_unverified","gpm"}; 52 const char *pl[] = {"sys_total","soc_pkg","cpu_e","cpu_p","gpu"}; 53 uint64_t prev[5] = {0}, nv_prev = 0, first, deadline; 54 unsigned long seconds; 55 char *end; 56 void *lib, *gpu = NULL; 57 int (*init)(void), (*shutdown_nv)(void), (*handle)(unsigned int, void **); 58 int (*energy_nv)(void *, unsigned long long *), (*power_nv)(void *, unsigned int *); 59 FILE *out; 60 int fd; 61 if (argc != 4) { fprintf(stderr,"usage: sample hwmon-dir seconds NEW-output.jsonl\n"); return 2; } 62 errno=0; seconds=strtoul(argv[2],&end,10); 63 if (errno || *end || seconds<1 || seconds>120) return 2; 64 for (int i=0;i<5;i++) { 65 check_label(argv[1],"energy",i+1,el[i]); 66 check_label(argv[1],"power",i+1,pl[i]); 67 } 68 lib=dlopen("libnvidia-ml.so.1",RTLD_NOW|RTLD_LOCAL); 69 if (!lib) { fprintf(stderr,"NVML: %s\n",dlerror()); return 1; } 70 #define SYM(dst,name) do { *(void **)(&(dst))=dlsym(lib,name); if (!(dst)) return 1; } while (0) 71 SYM(init,"nvmlInit_v2"); SYM(shutdown_nv,"nvmlShutdown"); 72 SYM(handle,"nvmlDeviceGetHandleByIndex_v2"); 73 SYM(energy_nv,"nvmlDeviceGetTotalEnergyConsumption"); SYM(power_nv,"nvmlDeviceGetPowerUsage"); 74 if (init() || handle(0,&gpu)) return 1; 75 fd=open(argv[3],O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC,0644); 76 if (fd<0) { perror(argv[3]); return 1; } 77 out=fdopen(fd,"w"); if (!out) return 1; 78 first=now_ns(); deadline=first; 79 for (unsigned int seq=0;seq<=seconds*10;seq++) { 80 uint64_t begin,end_ns,nv_begin,nv_end,e[5],p[5],ov[5]; 81 unsigned long long nv_mj; 82 unsigned int nv_mw; 83 struct timespec when={(time_t)(deadline/`1000000000`),(long)(deadline%`1000000000`)}; 84 int rc; 85 do { rc=clock_nanosleep(CLOCK_MONOTONIC,TIMER_ABSTIME,&when,NULL); } while(rc==EINTR); 86 if (rc) return 1; 87 begin=now_ns(); 88 if (begin-deadline>`100000000`) { fprintf(stderr,"sample missed deadline by >100 ms\n"); return 1; } 89 for (int i=0;i<5;i++) { 90 e[i]=read_value(argv[1],"energy",i+1,"input"); 91 ov[i]=read_value(argv[1],"energy",i+1,"overflow_raw"); 92 p[i]=read_value(argv[1],"power",i+1,"input"); 93 if (ov[i] || (seq && e[i]UINT32_MAX*UINT64_C(1000) || e[i]%1000 || p[i]>`600000000`) { 94 fprintf(stderr,"invalid/overflowed sensor %d\n",i+1); return 1; 95 } 96 /* Refuse a window that could wrap even at a 600 W upper bound. */ 97 if (!seq && UINT32_MAX*UINT64_C(1000)-e[i] < seconds*UINT64_C(`600000000`)) return 1; 98 prev[i]=e[i]; 99 } 100 end_ns=now_ns(); 101 if (end_ns-begin>`10000000`) { fprintf(stderr,"sensor read span >10 ms\n"); return 1; } 102 nv_begin=now_ns(); 103 if (energy_nv(gpu,&nv_mj) || power_nv(gpu,&nv_mw) || (seq && nv_mj