00001
00015 #include <stdlib.h>
00016 #include <stdio.h>
00017 #include <errno.h>
00018 #include <ctype.h>
00019 #include <time.h>
00020
00021 #include "gridlabd.h"
00022 #include "../tape/tape.h"
00023 #include "../tape/histogram.h"
00024 #include "tape_file.h"
00025
00026
00027
00028
00029 EXPORT int open_player(struct player *my, char *fname, char *flags)
00030 {
00031
00032 char *ff = fname;
00033
00034
00035 my->fp = (strcmp(fname,"-")==0?stdin:(ff?fopen(ff,flags):NULL));
00036 if (my->fp==NULL)
00037 {
00038 sprintf(my->lasterr, "player file %s: %s", fname, strerror(errno));
00039 my->status = TS_DONE;
00040 return 0;
00041 }
00042 else
00043 {
00044 my->loopnum = my->loop;
00045 my->status=TS_OPEN;
00046 my->type = FT_FILE;
00047 return 1;
00048 }
00049 }
00050
00051 EXPORT char *read_player(struct player *my,char *buffer,unsigned int size)
00052 {
00053 return fgets(buffer,size,my->fp);
00054 }
00055
00056 EXPORT int rewind_player(struct player *my)
00057 {
00058 return fseek(my->fp,SEEK_SET,0);
00059 }
00060
00061 EXPORT void close_player(struct player *my)
00062 {
00063 fclose(my->fp);
00064 my->status = TS_DONE;
00065 }
00066
00067
00068
00069
00070 #define MAPSIZE(N) ((N-1)/8+1)
00071 #define SET(X,B) ((X)[(B)/8]|=(1<<((B)&7)))
00072 #define ISSET(X,B) (((X)[(B)/8]&(1<<((B)&7)))==(1<<((B)&7)))
00073 char *file=NULL;
00074 int linenum=0;
00075 static int setmap(char *spec, unsigned char *map, int size)
00076 {
00077 char *p=spec;
00078 int last=-1;
00079 memset(map,0,MAPSIZE(size));
00080 while (*p!='\0')
00081 {
00082 if (*p=='*')
00083 {
00084 int i;
00085 for (i=0;i<size;i++)
00086 SET(map,i);
00087 p++;
00088 }
00089 else if (isdigit(*p))
00090 {
00091 int i=atoi(p);
00092 if (last<0)
00093
00094 SET(map,i);
00095 else if (i>last)
00096 do {
00097 SET(map,last);
00098 } while (++last<=i);
00099 else
00100 {
00101
00102 do {
00103 SET(map,last);
00104 } while (++last<size);
00105 last=0;
00106 do {
00107 SET(map,last);
00108 } while (++last<=i);
00109 }
00110 last = i;
00111 while (isdigit(*p)) p++;
00112 }
00113 else if (*p=='-')
00114 {
00115 p++;
00116 }
00117 else if (*p==';')
00118 {
00119 last = -1;
00120 p++;
00121 }
00122 else
00123 return 0;
00124 }
00125 return 1;
00126 }
00127 static unsigned char *hourmap(char *spec)
00128 {
00129 static unsigned char hours[MAPSIZE(24)];
00130 if (setmap(spec,hours,24))
00131 return hours;
00132 else
00133 return NULL;
00134 }
00135 static unsigned char *daymap(char *spec)
00136 {
00137 static unsigned char days[MAPSIZE(31)];
00138 if (setmap(spec,days,31))
00139 return days;
00140 else
00141 return NULL;
00142 }
00143 static unsigned char *monthmap(char *spec)
00144 {
00145 static unsigned char months[MAPSIZE(12)];
00146 if (setmap(spec,months,12))
00147 return months;
00148 else
00149 return NULL;
00150 }
00151 static unsigned char *weekdaymap(char *spec)
00152 {
00153 static unsigned char weekdays[MAPSIZE(7)];
00154 if (setmap(spec,weekdays,7))
00155 return weekdays;
00156 else
00157 return NULL;
00158 }
00159
00160 EXPORT int open_shaper(struct shaper *my, char *fname, char *flags)
00161 {
00162 char line[1024], group[256]="(unnamed)";
00163 float sum=0, load=0, peak=0;
00164 float scale[12][31][7][24];
00165
00166 char *ff = fname;
00167
00168
00169 memset(scale,0,sizeof(scale));
00170 linenum=0;
00171 file=fname;
00172
00173
00174 my->fp = (strcmp(fname,"-")==0?stdin:(ff?fopen(ff,flags):NULL));
00175 if (my->fp==NULL)
00176 {
00177 sprintf(my->lasterr, "shaper file %s: %s", fname, strerror(errno));
00178 goto Error;
00179 }
00180 my->status=TS_OPEN;
00181 my->type = FT_FILE;
00182
00183 my->step = 3600;
00184 my->interval = 24;
00185 memset(my->shape,0,sizeof(my->shape));
00186
00187 while (fgets(line,sizeof(line),my->fp)!=NULL)
00188 {
00189 unsigned char *hours, *days, *months, *weekdays;
00190 char min[256],hour[256],day[256],month[256],weekday[256],value[32];
00191 char *p=line;
00192 linenum++;
00193 while (isspace(*p)) p++;
00194 if (p[0]=='\0' || p[0]=='#') continue;
00195 if (strcmp(group,"")!=0 && (isdigit(p[0]) || p[0]=='*'))
00196 {
00197 int h, d, m, w;
00198 if (sscanf(line,"%s %s %s %s %[^,],%[^,\n]",min,hour,day,month,weekday,value)<6)
00199 {
00200 sprintf(my->lasterr, "%s(%d) : shape '%s' missing specification '%s'", file, linenum, group, line);
00201 goto Error;
00202 }
00203
00204 if (min[0]!='*')
00205 {
00206 sprintf(my->lasterr, "%s(%d) : minutes are ignored in '%s'", file, linenum, line);
00207 goto Error;
00208 }
00209 hours=hourmap(hour);
00210 if (hours==NULL)
00211 {
00212 sprintf(my->lasterr,"%s(%d): hours in '%s' not valid", file, linenum, line);
00213 goto Error;
00214 }
00215 days=daymap(day);
00216 if (days==NULL)
00217 {
00218 sprintf(my->lasterr,"%s(%d): days in '%s' not valid", file, linenum, line);
00219 goto Error;
00220 }
00221 months=monthmap(month);
00222 if (months==NULL)
00223 {
00224 sprintf(my->lasterr,"%s(%d): months in '%s' not valid", file, linenum, line);
00225 goto Error;
00226 }
00227 weekdays=weekdaymap(weekday);
00228 if (weekdays==NULL)
00229 {
00230 sprintf(my->lasterr,"%s(%d): weekdays in '%s' not valid", file, linenum, line);
00231 goto Error;
00232 }
00233 load = (float)atof(value);
00234 for (m=0; m<12; m++)
00235 {
00236 if (!ISSET(months,m)) continue;
00237 for (w=0; w<7; w++)
00238 {
00239 if (!ISSET(weekdays,w)) continue;
00240 for (d=0; d<31; d++)
00241 {
00242 if (!ISSET(days,d)) continue;
00243 for (h=0; h<24; h++)
00244 {
00245 if (!ISSET(hours,h)) continue;
00246 scale[m][d][w][h] = -load;
00247 }
00248 }
00249 }
00250 }
00251 sum += load;
00252 if (load>peak) peak=load;
00253 }
00254 else if (p[0]=='}')
00255 {
00256 int h, d, m, w;
00257 my->scale = peak/255/sum;
00258
00259 for (m=0; m<12; m++)
00260 {
00261 for (w=0; w<7; w++)
00262 {
00263 for (d=0; d<31; d++)
00264 {
00265 for (h=0; h<24; h++)
00266 {
00267 if (scale[m][d][w][h]<0)
00268 my->shape[m][d][w][h] = (unsigned char)(-scale[m][d][w][h] / peak * 255 +0.5);
00269 }
00270 }
00271 }
00272 }
00273 strcpy(group,"");
00274 }
00275 else if (sscanf(p,"%s {",group)==1)
00276 {
00277 sum=0;
00278 }
00279 else
00280 {
00281
00282 fprintf(stderr, "%s(%d) : shape specification '%s' is not valid", file, linenum, line);
00283 }
00284 }
00285 fclose(my->fp);
00286 my->fp=NULL;
00287 my->status = TS_OPEN;
00288 return 1;
00289 Error:
00290 if (my->fp)
00291 {
00292 fclose(my->fp);
00293 my->fp = NULL;
00294 }
00295 my->status = TS_ERROR;
00296 return 0;
00297 }
00298
00299 EXPORT char *read_shaper(struct shaper *my,char *buffer,unsigned int size)
00300 {
00301 return NULL;
00302 }
00303
00304 EXPORT int rewind_shaper(struct shaper *my)
00305 {
00306 return -1;
00307 }
00308
00309 EXPORT void close_shaper(struct shaper *my)
00310 {
00311 }
00312
00313
00314
00315
00316 EXPORT int open_recorder(struct recorder *my, char *fname, char *flags)
00317 {
00318 time_t now=time(NULL);
00319 OBJECT *obj=OBJECTHDR(my);
00320
00321 my->fp = (strcmp(fname,"-")==0?stdout:fopen(fname,flags));
00322 if (my->fp==NULL)
00323 {
00324
00325 fprintf(stderr, "recorder file %s: %s", fname, strerror(errno));
00326 my->status = TS_DONE;
00327 return 0;
00328 }
00329 my->type = FT_FILE;
00330 my->last.ts = TS_ZERO;
00331 my->status=TS_OPEN;
00332 my->samples=0;
00333
00334
00335 fprintf(my->fp,"# file...... %s\n", my->file);
00336 fprintf(my->fp,"# date...... %s", asctime(localtime(&now)));
00337 #ifdef WIN32
00338 fprintf(my->fp,"# user...... %s\n", getenv("USERNAME"));
00339 fprintf(my->fp,"# host...... %s\n", getenv("MACHINENAME"));
00340 #else
00341 fprintf(my->fp,"# user...... %s\n", getenv("USER"));
00342 fprintf(my->fp,"# host...... %s\n", getenv("HOST"));
00343 #endif
00344 if(obj->parent){
00345 fprintf(my->fp,"# target.... %s %d\n", obj->parent->oclass->name, obj->parent->id);
00346 }
00347 fprintf(my->fp,"# trigger... %s\n", my->trigger[0]=='\0'?"(none)":my->trigger);
00348 fprintf(my->fp,"# interval.. %d\n", my->interval);
00349 fprintf(my->fp,"# limit..... %d\n", my->limit);
00350 fprintf(my->fp,"# timestamp,%s\n", my->property);
00351
00352 return 1;
00353 }
00354
00355 EXPORT int write_recorder(struct recorder *my, char *timestamp, char *value)
00356 {
00357 return fprintf(my->fp,"%s,%s\n", timestamp, value);
00358 }
00359
00360 EXPORT void close_recorder(struct recorder *my)
00361 {
00362 if (my->fp)
00363 {
00364 fprintf(my->fp,"# end of tape\n");
00365 fclose(my->fp);
00366 my->fp = NULL;
00367
00368 }
00369 }
00370
00371
00372
00373
00374 EXPORT int open_histogram(histogram *my, char *fname, char *flags)
00375 {
00376 time_t now=time(NULL);
00377 OBJECT *obj=OBJECTHDR(my);
00378
00379 my->fp = (strcmp(fname,"-")==0?stdout:fopen(fname,"w"));
00380 if (my->fp==NULL)
00381 {
00382
00383 fprintf(stderr, "histogram file %s: %s", fname, strerror(errno));
00384 return 0;
00385 }
00386 my->type = FT_FILE;
00387 my->t_count = TS_ZERO;
00388 my->t_sample = TS_ZERO;
00389
00390
00391 fprintf(my->fp,"# file...... %s\n", my->fname);
00392 fprintf(my->fp,"# date...... %s", asctime(localtime(&now)));
00393 #ifdef WIN32
00394 fprintf(my->fp,"# user...... %s\n", getenv("USERNAME"));
00395 fprintf(my->fp,"# host...... %s\n", getenv("MACHINENAME"));
00396 #else
00397 fprintf(my->fp,"# user...... %s\n", getenv("USER"));
00398 fprintf(my->fp,"# host...... %s\n", getenv("HOST"));
00399 #endif
00400 if(obj->parent != NULL){
00401 fprintf(my->fp,"# target.... %s %d\n", obj->parent->oclass->name, obj->parent->id);
00402 } else {
00403 fprintf(my->fp,"# group.... %s\n", my->group);
00404 }
00405
00406 fprintf(my->fp,"# counting interval.. %d\n", my->counting_interval);
00407 fprintf(my->fp,"# sampling interval.. %d\n", my->sampling_interval);
00408 fprintf(my->fp,"# limit..... %d\n", my->limit);
00409 if(my->bins[0] != 0){
00410 fprintf(my->fp,"# timestamp,%s\n", my->bins);
00411 } else {
00412 int i = 0;
00413 for(i = 0; i < my->bin_count; ++i){
00414 fprintf(my->fp,"%s%f..%f%s",(my->bin_list[i].low_inc ? "[" : "("), my->bin_list[i].low_val, my->bin_list[i].high_val, (my->bin_list[i].high_inc ? "]" : ")"));
00415 if(i+1 < my->bin_count){
00416 fprintf(my->fp,",");
00417 }
00418 }
00419 fprintf(my->fp, "\n");
00420 }
00421
00422
00423 return 1;
00424 }
00425
00426 EXPORT int write_histogram(histogram *my, char *timestamp, char *value)
00427 {
00428 return fprintf(my->fp,"%s,%s\n", timestamp, value);
00429 }
00430
00431 EXPORT void close_histogram(histogram *my)
00432 {
00433 if (my->fp)
00434 {
00435 fprintf(my->fp,"# end of tape\n");
00436 fclose(my->fp);
00437 my->fp = NULL;
00438
00439
00440
00441 }
00442 }
00443
00444
00445
00446
00447 EXPORT int open_collector(struct collector *my, char *fname, char *flags)
00448 {
00449 unsigned int count=0;
00450 time_t now=time(NULL);
00451
00452 my->fp = (strcmp(fname,"-")==0?stdout:fopen(fname,flags));
00453 if (my->fp==NULL)
00454 {
00455
00456 fprintf(stderr, "collector file %s: %s", fname, strerror(errno));
00457 my->status = TS_DONE;
00458 return 0;
00459 }
00460 my->last.ts = TS_ZERO;
00461 my->status=TS_OPEN;
00462 my->type = FT_FILE;
00463 my->samples=0;
00464
00465
00466 count += fprintf(my->fp,"# file...... %s\n", my->file);
00467 count += fprintf(my->fp,"# date...... %s", asctime(localtime(&now)));
00468 #ifdef WIN32
00469 count += fprintf(my->fp,"# user...... %s\n", getenv("USERNAME"));
00470 count += fprintf(my->fp,"# host...... %s\n", getenv("MACHINENAME"));
00471 #else
00472 count += fprintf(my->fp,"# user...... %s\n", getenv("USER"));
00473 count += fprintf(my->fp,"# host...... %s\n", getenv("HOST"));
00474 #endif
00475 count += fprintf(my->fp,"# group..... %s\n", my->group);
00476 count += fprintf(my->fp,"# trigger... %s\n", my->trigger[0]=='\0'?"(none)":my->trigger);
00477 count += fprintf(my->fp,"# interval.. %d\n", my->interval);
00478 count += fprintf(my->fp,"# limit..... %d\n", my->limit);
00479 count += fprintf(my->fp,"# property.. timestamp,%s\n", my->property);
00480
00481 return count;
00482 }
00483
00484 EXPORT int write_collector(struct collector *my, char *timestamp, char *value)
00485 {
00486 return fprintf(my->fp,"%s,%s\n", timestamp, value);
00487 }
00488
00489 EXPORT void close_collector(struct collector *my)
00490 {
00491 if (my->fp)
00492 {
00493 fprintf(my->fp,"# end of tape\n");
00494 fclose(my->fp);
00495 }
00496 my->fp = 0;
00497 }
00498