文件数据分析比较(Rev 02!)大佬有更好的思路请多多指教!!!
本帖最后由 Cool_Breeze 于 2020-4-10 11:05 编辑感谢red2020--_--||
#if 0
By Cool_Breeze
Rev 02
#endif
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <malloc.h>
#include <float.h>
#include <errno.h>
typedef unsigned long long intll;
FILE* fileptr(char* file, char* mode);
//源文件解析
void scantext(FILE* fp, intll* inu);
void getname(FILE* fp, char* name, intll si);
void getdata(FILE* fp, double* max, double* min, double* avg, double* sum, double* count,\
intll ns, intll nl, char* max_n, char* min_n);
//limit文件解析
void limittext(FILE* fp, intll* inu);
void getlimitdata(FILE* fp,char* name, char* item, char* comp, double* data);
int compare(char *cmp,double s,double l);
void output(FILE* fp,int flag,int n,char* name,char* litem,double sdata,char* comp,double ldata);
#define LINES 1024
#define NAMESIZE 100
#define COMPSIZE 3
static char temp;
static char const delim[]=",";
static int ERROR=0;//ERROR退出值,0代表Pass ,非0代表Fail
int main(int argc,char **argv)
{
static char usage="-s source.csv\n-l limit.csv\n-r result.csv";
static char source;
static char limit;
static char result;
register int opt=0;
while ((opt = getopt(argc,argv,"-s:-l:-r:")) != -1)
switch (opt)
{
case 's': strcpy(source,optarg);break;
case 'l': strcpy(limit,optarg);break;
case 'r': strcpy(result,optarg);break;
default : printf("%s\n",usage);exit(EXIT_SUCCESS);
}
FILE* fpi=fileptr(source,"rb");
FILE* lpi=fileptr(limit,"rb");
FILE* fpo=fileptr(result,"wb");
//开始源文件解析
static intll inu,isize=NAMESIZE;
scantext(fpi,&inu);
char* iname=(char*)calloc(inu,sizeof(char)*isize);
char* max_n=(char*)calloc(inu,sizeof(char)*isize);
char* min_n=(char*)calloc(inu,sizeof(char)*isize);
getname(fpi, iname, isize);
double* max=(double*)calloc(inu,sizeof(double));
double* min=(double*)calloc(inu,sizeof(double));
double* avg=(double*)calloc(inu,sizeof(double));
double* sum=(double*)calloc(inu,sizeof(double));
double* count=(double*)calloc(inu,sizeof(double));
register intll i=0;
for (i=0;i<inu;i++) *(min+i)=DBL_MAX, *(max+i)=LDBL_MIN;
getdata(fpi, max, min, avg, sum, count, isize, inu, max_n, min_n);
//源文件解析完成
//开始limit文件解析
static intll lnu,lsize=NAMESIZE;
limittext(lpi,&lnu);
char* lname=(char*)calloc(lnu,sizeof(char)*lsize);
char* litem=(char*)calloc(lnu,sizeof(char)*lsize);
char* comp=(char*)calloc(lnu,sizeof(char)*COMPSIZE);
double* ldata=(double*)calloc(lnu,sizeof(double)*lsize);
getlimitdata(lpi, lname, litem, comp, ldata);
//limit文件解析完成
static const char li_comp={{"Max"},{"Min"},{"Avg"}};
register int j=0,flag=1;
register int k=0;
fprintf(fpo," Result\r\n");
fprintf(fpo," Nu, Item, Options, %s, Comp, %s, Result\r\n",source,limit);
for (i=0;i<lnu;i++)
for (j=0;j<inu-1;j++)
if ( ! strcmp(lname+i*NAMESIZE,iname+(j+1)*NAMESIZE) )//匹配项目名 (j+1)跳过第一项数据生成时间项
{
for (k=0;k<3;k++) if ( ! strcmp(litem+i*NAMESIZE,li_comp) ) break;
switch (k)
{
case 0: flag=compare(comp+i*COMPSIZE,*(max+j),*(ldata+i));
output(fpo,flag,(j+1),lname+i*NAMESIZE,litem+i*NAMESIZE,max,comp+i*COMPSIZE,ldata);
break;
case 1: flag=compare(comp+i*COMPSIZE,*(min+j),*(ldata+i));
output(fpo,flag,(j+1),lname+i*NAMESIZE,litem+i*NAMESIZE,min,comp+i*COMPSIZE,ldata);
break;
case 2: flag=compare(comp+i*COMPSIZE,*(avg+j),*(ldata+i));
output(fpo,flag,(j+1),lname+i*NAMESIZE,litem+i*NAMESIZE,avg,comp+i*COMPSIZE,ldata);
break;
default : break;
}
}
//输出源文件和limit文件内容
fprintf(fpo," %s\r\n",limit);
fprintf(fpo," Nu, Item, Comp_Item, Comp, Data\r\n");
for (i=0;i<lnu;i++)fprintf(fpo,"%-d, %s, %s, %s,%lf\r\n",i+1,lname+i*NAMESIZE,litem+i*NAMESIZE,comp+i*COMPSIZE,*(ldata+i));
fprintf(fpo," %s\r\n",source);
fprintf(fpo," Nu, Item, Count, Max, Max_Date, Min, Min_Date, Sum, Avg\r\n");
for (i=0;i<inu-1;i++) fprintf(fpo,"%-d, %s, %.0lf, %.6lf, %s, %.6lf, %s, %.6lf, %.6lf\r\n",\
i+1,iname+(i+1)*isize,*(count+i),*(max+i),max_n+i*isize,*(min+i),min_n+i*isize,*(sum+i),*(avg+i));
free(iname);
free(max);
free(min);
free(avg);
free(sum);
free(count);
free(max_n);
free(min_n);
free(lname);
free(litem);
free(comp);
free(ldata);
fclose(fpi);
fclose(fpo);
return ERROR;
}
FILE* fileptr(char* file, char* mode)
{
FILE* fp = fopen(file,mode);
if (fp == NULL)
{
printf("open file fail!:%s",strerror(errno));
exit(EXIT_FAILURE);
}
return fp;
}
void scantext(FILE* fp, intll* inu)
{
rewind(fp);
register intll i=0;//记录项目数
char *p;
fgets(temp,LINES,fp);
p=strtok(temp,delim);
while(p != NULL)
{
i++;
p=strtok(NULL,delim);
}
*inu=i;
}
void getname(FILE* fp, char* name, intll si)
{
rewind(fp);
fgets(temp,LINES,fp);
char* p=strtok(temp,delim);
register intll i=0,l;
while (p != NULL)
{
strcpy(name+i*si,p);
p=strtok(NULL,delim);
i++;
}
i--;
l=strlen(name+i*si);
while (l--)
if (*(name+i*si+l) == 0xA || *(name+i*si+l) == 0xD)//最后一列丢弃换行,回车
*(name+i*si+l) = '\0';
else break;
}
void getdata(FILE* fp, double* max, double* min, double* avg, double* sum, double* count,\
intll ns, intll nl, char* max_n, char* min_n)
{
rewind(fp);
fgets(temp,LINES,fp);
register double t=0;
register intll i=0;
register intll cou=0;
register intll stl=0;
char *p=NULL;
char *pb=NULL;
while (fgets(temp,LINES,fp))
{
cou++;
i=0;
p=pb=strtok(temp,delim);//按指定字符分割字符串 ,pb指向字符串的第一列数据
if ( cou == 1 )
{
int i;
stl=strlen(pb);
for (i=0;i<nl-1;i++)
{
memmove(max_n+i*ns,pb,stl);
*(max_n+i*ns+stl)='\0';
memmove(min_n+i*ns,pb,stl);
*(min_n+i*ns+stl)='\0';
}
}
do
{
p=strtok(NULL,delim);//遍历每一项数据
if (p == NULL) break;
t=atof(p);
stl=strlen(pb);
if (t > *(max+i))
{
*(max+i) = t;
memmove(max_n+i*ns,pb,stl);
*(max_n+i*ns+stl)='\0';
}
if (t < *(min+i))
{
*(min+i) = t;
memmove(min_n+i*ns,pb,stl);
*(min_n+i*ns+stl)='\0';
}
*(sum+i)+=t;
i++;
}while (1);
}
register intll j;
for (j=0; j<nl-1; j++)//如果最后一行字符串小于标准列数,单独计算平均值和计数器
{
if (j < i) *(avg+j)=*(sum+j)/cou,*(count+j)=cou;
else *(avg+j)=*(sum+j)/(cou-1),*(count+j)=cou-1;
}
}
//limit文件解析
void limittext(FILE* fp, intll* inu)
{
rewind(fp);
register intll i=0;
while (fgets(temp,LINES,fp))
i++;//读取行数
*inu=i;
}
void getlimitdata(FILE* fp,char* name, char* item, char* comp, double* data)
{
rewind(fp);
char* p=temp;
register intll i=0,count=0,psize=0;
while (fgets(temp,LINES,fp))
{
count=0;
p=strtok(temp,delim);
do
{
if(p == NULL) break;
psize=strlen(p);//按分割每个符分割字符串
switch (count)
{
case 0: memmove(name+i*NAMESIZE,p,psize);
*(name+i*NAMESIZE+psize)='\0';break;
case 1: memmove(item+i*NAMESIZE,p,psize);
*(item+i*NAMESIZE+psize)='\0';break;
case 2: memmove(comp+i*COMPSIZE,p,psize);
*(comp+i*COMPSIZE+psize)='\0';break;
case 3: *(data+i) = atof(p);break;
default : break;
}
count++;
} while (p=strtok(NULL,delim));
i++;
}
}
//数据比较
int compare(char *cmp,double s,double l)
{
if (! strcmp(cmp,"<")) return (s < l)? 1:0;
else if (! strcmp(cmp,">")) return (s > l)? 1:0;
else if (! strcmp(cmp,"=")) return (s = l)? 1:0;
else if (! strcmp(cmp,"<=")) return (s <= l)? 1:0;
else if (! strcmp(cmp,">=")) return (s >= l)? 1:0;
}
//输出
void output(FILE* fp,int flag,int n,char* name,char* litem,double sdata,char* comp,double ldata)
{
if (flag)
fprintf(fp,"%-d, %s, %s,%.5lf, %s,%.5lf, Pass,\r\n",n,name,litem,sdata,comp,ldata);
else
{
fprintf(fp,"%-d, %s, %s,%.5lf, %s,%.5lf, Fail,\r\n",n,name,litem,sdata,comp,ldata);
ERROR++;
}
}
源文件一部分:time stamp, GPU Temperature (C) (C), Junction Temperature (C) (C), Mem Temperature (C) (C), VR_GFX (C) (C), VR_SOC (C) (C), VR_MEM (C) (C), VR_VDDCI (C) (C), Liquid0 (C) (C), Liquid1 (C) (C), PLX (C) (C), Min (C) (C), GFXCLK Freq (), PWM (), FAN Speed (), Limit PPT0 (W) (W), Value PPT0 (W) (W), GFX Activity (%) (%), PCIe Link Speed (GT/s) (GT/s), PCIe Link Width (), PCIe Correctable Error (), PCIe Uncorrectable Error (), PCIe Residency Gen 1 (%) (%), PCIe Residency Gen 2 (%) (%), PCIe Residency Gen 3 (%) (%), PCIe Residency Gen 4 (%) (%), PCIe Residency L0 (%) (%), PCIe Residency L0s (%) (%), PCIe Residency L1 (%) (%), Fan PWM reading [%] (%), mclk (MHz), sclk (MHz)
00:41:43.292, 41.36250, 41.36250, 42.00000, 33.00000, 34.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 38.85000, 783.68744, 0.00000, 124.55593, 165.00000, 12.23484, 28.04605, 8.00000, 16.00000, 0.00000, 0.00000, 0.00000, 0.00000, 100.00000, 0.00000, 100.00000, 0.00000, 0.00000, 0.00000, 101.0,781.0
00:41:44.339, 40.57681, 40.58622, 42.00000, 33.00000, 34.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 38.50471, 783.88538, 0.00000, 124.55510, 165.00000, 9.72985, 1.56070, 8.00000, 16.00000, 0.00000, 0.00000, 60.00000, 0.00000, 40.00000, 0.00000, 100.00000, 0.00000, 0.00000, 0.00000, 101.0,783.0
00:41:45.371, 42.58641, 45.18058, 42.00000, 33.00000, 34.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 39.46432, 991.20605, 1.87348, 124.55509, 165.00000, 40.61980, 39.77980, 8.00000, 16.00000, 0.00000, 0.00000, 30.00000, 0.00000, 70.00000, 0.00000, 100.00000, 0.00000, 0.00000, 15.00000, 876.0,1532.0
00:41:46.404, 51.70063, 63.22072, 48.43563, 33.56438, 34.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 45.59245, 1548.99268, 14.84375, 321.30173, 165.00000, 164.99956, 96.44620, 8.00000, 16.00000, 0.00000, 0.00000, 20.68966, 0.00000, 79.31035, 0.00000, 100.00000, 0.00000, 0.00000, 15.00000, 876.0,1547.0
00:41:47.436, 53.12355, 64.55281, 51.67248, 34.53392, 34.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 46.97141, 1545.35413, 14.84375, 934.19299, 165.00000, 165.00191, 96.96519, 8.00000, 16.00000, 0.00000, 0.00000, 15.38462, 0.00000, 84.61539, 0.00000, 100.00000, 0.00000, 0.00000, 15.00000, 876.0,1541.0
00:41:48.468, 53.81395, 65.14874, 52.00000, 35.13857, 34.57849, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 47.64244, 1545.16492, 14.84375, 697.11292, 165.00000, 164.99620, 97.30402, 8.00000, 16.00000, 0.00000, 0.00000, 12.50000, 0.00000, 87.50000, 0.00000, 100.00000, 0.00000, 0.00000, 15.00000, 876.0,1531.0
00:41:49.501, 54.39037, 65.75484, 52.00000, 36.00000, 35.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 48.25992, 1553.07312, 14.84375, 516.50922, 165.00000, 164.99942, 95.85622, 8.00000, 16.00000, 0.00000, 0.00000, 10.52632, 0.00000, 89.47369, 0.00000, 100.00000, 0.00000, 0.00000, 15.00000, 876.0,1538.0
00:41:50.533, 54.87136, 66.29603, 53.86047, 36.32170, 35.00000, 0.00000, 0.00000, 0.00000, 0.00000, 0.00000, 48.84981, 1548.97766, 15.06767, 457.48981, 165.00000, 164.96523, 95.95676, 8.00000, 16.00000, 0.00000, 0.00000, 8.95522, 0.00000, 91.04478, 0.00000, 100.00000, 0.00000, 0.00000, 16.00000, 876.0,1576.0 本帖最后由 Cool_Breeze 于 2020-4-10 11:01 编辑
限制文件: VDDCR_GFX Current (A) (A),Avg,>,150
GPU Temperature (C) (C),Max,<,88
Junction Temperature (C) (C),Min,<=,108
Mem Temperature (C) (C),Max,<=,120
Limit PPT0 (W) (W),Avg,<,300
PCIe Link Speed (GT/s) (GT/s),Max,>=,8
Fan PWM reading [%] (%),Min,<=,2150
mclk (MHz),Min,>=,56
sclk (MHz),Max,>,66
本帖最后由 Cool_Breeze 于 2020-4-10 11:02 编辑
Result
Nu, Item, Options, tes.csv, Comp, limit.csv, Result
1,VDDCR_GFX Current (A) (A), Avg,123.75536, >,150.00000, Fail,
2,GPU Temperature (C) (C), Max,84.67364, <,88.00000, Pass,
3,Junction Temperature (C) (C), Min,36.78448, <=,108.00000, Pass,
4,Mem Temperature (C) (C), Max,98.00000, <=,120.00000, Pass,
16,Limit PPT0 (W) (W), Avg,195.00000, <,300.00000, Pass,
19,PCIe Link Speed (GT/s) (GT/s), Max,8.00000, >=,8.00000, Pass,
30,Fan PWM reading [%] (%), Min,0.00000, <=,2150.00000, Pass,
31,mclk (MHz), Min,101.00000, >=,56.00000, Pass,
32,sclk (MHz), Max,2084.00000, >,66.00000, Pass,
limit.csv
Nu, Item, Comp_Item, Comp, Data
1,VDDCR_GFX Current (A) (A), Avg, >,150.000000
2,GPU Temperature (C) (C), Max, <,88.000000
3,Junction Temperature (C) (C), Min, <=,108.000000
4,Mem Temperature (C) (C), Max, <=,120.000000
5,Limit PPT0 (W) (W), Avg, <,300.000000
6,PCIe Link Speed (GT/s) (GT/s), Max, >=,8.000000
7,Fan PWM reading [%] (%), Min, <=,2150.000000
8,mclk (MHz), Min, >=,56.000000
9,sclk (MHz), Max, >,66.000000
tes.csv
Nu, Item, Count, Max, Max_Date, Min, Min_Date, Sum, Avg
1,VDDCR_GFX Current (A) (A), 36384, 244.956880, 20:32:05.864, 2.007840, 19:29:01.189, 4502714.966540, 123.755359
2,GPU Temperature (C) (C), 36384, 84.673640, 22:06:03.904, 36.745070, 19:29:01.189, 2933954.125140, 80.638581
3,Junction Temperature (C) (C), 36384, 99.827960, 22:06:07.134, 36.784480, 19:29:01.189, 3387563.903270, 93.105868
4,Mem Temperature (C) (C), 36384, 98.000000, 22:06:16.874, 34.000000, 19:29:02.245, 3384580.249270, 93.023863
5,VR_GFX (C) (C), 36384, 85.000000, 22:06:15.774, 27.000000, 19:29:01.189, 2967047.724460, 81.548145
6,VR_SOC (C) (C), 36384, 79.000000, 22:05:59.594, 29.000000, 19:29:01.189, 2790556.417030, 76.697351
7,VR_MEM (C) (C), 36384, 0.000000, 19:29:01.189, 0.000000, 19:29:01.189, 0.000000, 0.000000
8,VR_VDDCI (C) (C), 36384, 0.000000, 19:29:01.189, 0.000000, 19:29:01.189, 0.000000, 0.000000
9,Liquid0 (C) (C), 36384, 0.000000, 19:29:01.189, 0.000000, 19:29:01.189, 0.000000, 0.000000
10,Liquid1 (C) (C), 36384, 0.000000, 19:29:01.189, 0.000000, 19:29:01.189, 0.000000, 0.000000
11,PLX (C) (C), 36384, 0.000000, 19:29:01.189, 0.000000, 19:29:01.189, 0.000000, 0.000000
12,Min (C) (C), 36384, 79.158970, 22:06:02.803, 35.354680, 19:29:01.189, 2755640.988070, 75.737714
13,GFXCLK Freq (), 36384, 2067.620360, 05:42:38.450, 804.097780, 00:42:47.646, 69755491.926140, 1917.202395
14,PWM (), 36384, 60.548110, 22:06:21.194, 0.000000, 19:29:01.189, 1782709.728940, 48.997079
15,FAN Speed (), 36384, 2452.709470, 22:06:26.614, 0.000000, 19:29:01.189, 78159803.685400, 2148.191614
16,Limit PPT0 (W) (W), 36384, 195.000000, 19:29:01.189, 195.000000, 19:29:01.189, 7094880.000000, 195.000000
17,Value PPT0 (W) (W), 36384, 195.003540, 02:33:50.597, 10.208000, 19:29:01.189, 6370334.954680, 175.086163
18,GFX Activity (%) (%), 36384, 99.975200, 00:42:08.825, 0.073530, 00:51:53.411, 3120834.797830, 85.774923
19,PCIe Link Speed (GT/s) (GT/s), 36384, 8.000000, 19:29:01.189, 2.500000, 19:29:02.245, 289350.500000, 7.952685
20,PCIe Link Width (), 36384, 16.000000, 19:29:01.189, 16.000000, 19:29:01.189, 582144.000000, 16.000000
21,PCIe Correctable Error (), 36384, 0.000000, 19:29:01.189, 0.000000, 19:29:01.189, 0.000000, 0.000000
22,PCIe Uncorrectable Error (), 36384, 0.000000, 19:29:01.189, 0.000000, 19:29:01.189, 0.000000, 0.000000
23,PCIe Residency Gen 1 (%) (%), 36384, 51.898740, 19:29:09.790, 2.522750, 19:33:32.379, 114556.277570, 3.148534
24,PCIe Residency Gen 2 (%) (%), 36384, 0.000000, 19:29:01.189, 0.000000, 19:29:01.189, 0.000000, 0.000000
25,PCIe Residency Gen 3 (%) (%), 36384, 97.477250, 19:33:32.379, 48.101270, 19:29:09.790, 3523843.723860, 96.851466
26,PCIe Residency Gen 4 (%) (%), 36384, 0.000000, 19:29:01.189, 0.000000, 19:29:01.189, 0.000000, 0.000000
27,PCIe Residency L0 (%) (%), 36384, 100.000000, 19:29:01.189, 99.997400, 20:40:54.996, 3638374.158400, 99.999290
28,PCIe Residency L0s (%) (%), 36384, 0.000000, 19:29:01.189, 0.000000, 19:29:01.189, 0.000000, 0.000000
29,PCIe Residency L1 (%) (%), 36384, 0.000000, 19:29:01.189, 0.000000, 19:29:01.189, 0.000000, 0.000000
30,Fan PWM reading [%] (%), 36384, 60.000000, 19:32:11.820, 0.000000, 19:29:01.189, 1782582.000000, 48.993569
31,mclk (MHz), 36383, 32327430.000000, 01:14:48.400, 101.000000, 19:29:01.189, 62883779.700000, 1728.383577
32,sclk (MHz), 36383, 2084.000000, 22:58:23.259, 796.000000, 19:56:22.313, 69639761.000000, 1914.074183
页:
[1]