15#define CALLOC(num, type) (type*)calloc((num),sizeof(type))
16#define FREE(ptr) free(ptr); (ptr)=NULL;
45void write_hist_speed()
48 DWORD start_time, act_time;
57 for (
i = 0;
i < 100;
i++) {
64 bytes += (
sizeof(hist) +
sizeof(hv)) *
i;
69 }
while (act_time - start_time < 10000);
71 printf(
"%d events (%d kB) per sec.\n",
j / (act_time - start_time) * 1000,
72 bytes / 1024 / (act_time - start_time) * 1000);
86 for (
i = 0;
i < 10;
i++) {
91 for (
j = 0;
j < 100;
j++)
102 std::string* event_name,
113 std::vector<std::string> events;
117 printf(
"hs_get_events() error %d\n",
status);
121 printf(
"Available events:\n");
122 for (
unsigned i = 0;
i < events.size();
i++) {
123 printf(
"%d: %s\n",
i, events[
i].c_str());
126 if (events.size() == 0)
128 else if (events.size() == 1)
129 *event_name = events[0];
132 printf(
"\nSelect event: ");
133 rd = scanf(
"%d", &
i);
136 *event_name = events[
i];
139 std::vector<TAG> tags;
143 printf(
"hs_get_tags() error %d\n",
status);
147 printf(
"\nAvailable variables:\n");
149 for (
unsigned j = 0;
j < tags.size();
j++)
150 if (tags[
j].n_data > 1)
151 printf(
"%d: %s[%d]\n",
j, tags[
j].
name, tags[
j].n_data);
153 printf(
"%d: %s\n",
j, tags[
j].
name);
155 *
index = var_index = 0;
156 if (tags.size() > 1) {
157 printf(
"\nSelect variable (0..%d,-1 for all): ", (
int)tags.size() - 1);
158 rd = scanf(
"%d", &var_index);
161 if (var_index >= (
int)tags.size())
162 var_index = tags.size() - 1;
169 if (var_index >= 0) {
171 *var_type = tags[var_index].type;
172 *var_n_data = tags[var_index].n_data;
174 if (tags[var_index].n_data > 1 && tags[var_index].
type !=
TID_STRING) {
175 printf(
"\nSelect index (0..%d): ", tags[var_index].n_data - 1);
176 rd = scanf(
"%d",
index);
182 printf(
"\nHow many hours: ");
183 rd = scanf(
"%d", &hour);
187 *start_time = (*end_time) - hour * 3600;
189 printf(
"\nInterval [sec]: ");
190 rd = scanf(
"%d", interval);
202 DWORD status,
i,
j, bytes,
n, nv, ltime, n_bytes, name_size, id_size;
208 memset(&tms, 0,
sizeof(tms));
211 if (tms.tm_year < 90)
223 id_size =
n *
sizeof(
INT);
224 char* event_name = (
char*)malloc(name_size);
227 (
DWORD *) & id_size);
229 for (
i = 0;
i <
n;
i++) {
233 n_bytes = nv *
sizeof(
DWORD);
234 char* var_names = (
char*)malloc(bytes);
238 for (
j = 0;
j < nv;
j++)
292 n =
read(fh, (
char *) &rec,
sizeof(rec));
296 printf(
"Error reading \"%s\", errno %d (%s)\n",
file_name, errno, strerror(errno));
299 if (
n != (
int)
sizeof(rec)) {
300 printf(
"Error reading \"%s\", truncated data, requested %d bytes, read %d bytes\n",
file_name, (
int)
sizeof(rec),
n);
305 if (rec.record_type ==
RT_DEF) {
307 n =
read(fh, event_name,
sizeof(event_name));
308 if (
n !=
sizeof(event_name)) {
309 printf(
"Error reading \"%s\", truncated data or error, requested %d bytes, read %d bytes, errno %d (%s)\n",
file_name, (
int)
sizeof(rec),
n, errno, strerror(errno));
313 if (rec.event_id ==
id ||
id == 0)
314 printf(
"Event definition %s, ID %d\n", event_name, rec.event_id);
317 lseek(fh, rec.data_size, SEEK_CUR);
322 sprintf(
str,
"%d ", rec.time);
324 ltime = (time_t) rec.time;
326 ctime_r(<ime, ctimebuf);
327 mstrlcpy(
str, ctimebuf + 4,
sizeof(
str));
330 if (rec.event_id ==
id ||
id == 0)
331 printf(
"ID %d, %s, size %d\n", rec.event_id,
str, rec.data_size);
334 lseek(fh, rec.data_size, SEEK_CUR);
346 std::vector<std::string> events;
350 printf(
"hs_get_events() error %d\n",
status);
354 for (
unsigned i = 0;
i < events.size();
i++) {
355 printf(
"\nEvent \'%s\'\n", events[
i].c_str());
357 std::vector<TAG> tags;
361 printf(
"hs_get_tags() error %d\n",
status);
365 for (
unsigned j = 0;
j < tags.size();
j++)
366 if (tags[
j].n_data > 1)
367 printf(
"%d: %s[%d]\n",
j, tags[
j].
name, tags[
j].n_data);
369 printf(
"%d: %s\n",
j, tags[
j].
name);
377 const char* event_name,
378 time_t start_time, time_t end_time, time_t interval,
382 time_t *tbuffer = NULL;
383 double *buffer = NULL;
388 status =
mh->
hs_read(start_time, end_time, interval, 1, &event_name, &
var_name, &
index, &
n, &tbuffer, &buffer, &hs_status);
391 printf(
"hs_read() error %d\n",
status);
396 printf(
"hs_read() cannot read event, status %d\n", hs_status);
401 printf(
"No data for event \"%s\" variable \"%s\" found in specified time range\n", event_name,
var_name);
403 for (
int i = 0;
i <
n;
i++) {
407 sprintf(
str,
"%d %.16g", (
int)tbuffer[
i], buffer[
i]);
410 ctime_r(&tbuffer[
i], ctimebuf);
411 sprintf(
str,
"%s %.16g", ctimebuf + 4, buffer[
i]);
414 char* s = strchr(
str,
'\n');
428 const char* event_name,
429 time_t start_time, time_t end_time, time_t interval,
430 const char *
var_name,
int index1,
int index2)
435 if (index2 < index1) {
436 printf(
"Wrong specified range (low>high)\n");
440 int nvars = index2 - index1 + 1;
442 const char** en =
CALLOC(nvars,
const char*);
443 const char** vn =
CALLOC(nvars,
const char*);
444 int* in =
CALLOC(nvars,
int);
446 for (
int i=0;
i<nvars;
i++) {
453 time_t** tbuffer =
CALLOC(nvars, time_t*);
454 double** vbuffer =
CALLOC(nvars,
double*);
455 int* hs_status =
CALLOC(nvars,
int);
457 status =
mh->
hs_read(start_time, end_time, interval, nvars, en, vn, in,
n, tbuffer, vbuffer, hs_status);
460 printf(
"Cannot read history data, hs_read() error %d\n",
status);
464 for (
int i=0;
i<nvars;
i++) {
466 printf(
"No data for event \"%s\" variable \"%s\" index %d found in specified time range\n", event_name,
var_name, index1+
i);
468 printf(
"Cannot read event \"%s\" variable \"%s\" index %d, status %d\n", event_name,
var_name, index1+
i, hs_status[
i]);
471 printf(
"mhist for Var:%s[%d:%d]\n",
var_name, index1, index2);
472 for (
int i = 0;
i <
n[0];
i++) {
474 printf(
"%d ", (
int)tbuffer[0][
i]);
477 time_t ltime = (time_t) tbuffer[0][
i];
479 ctime_r(<ime, ctimebuf);
480 sprintf(
str,
"%s", ctimebuf + 4);
485 for (
int j = 0, idx = index1; idx < index2 + 1; idx++,
j++) {
487 printf(
"%.16g", vbuffer[
j][
i]);
493 for (
int i=0;
i<nvars;
i++) {
511 const char* event_name,
512 time_t start_time, time_t end_time, time_t interval)
517 std::vector<TAG> tags;
521 printf(
"Cannot get list of variables for event \'%s\', hs_get_tags() error %d\n", event_name,
status);
525 for (
unsigned j = 0;
j < tags.size();
j++) {
526 if (tags[
j].n_data > 1)
527 printf(
"%d: %s[%d]\n",
j, tags[
j].
name, tags[
j].n_data);
529 printf(
"%d: %s\n",
j, tags[
j].
name);
534 for (
unsigned i=0;
i<tags.size();
i++) {
535 if (tags[
i].n_data == 1) {
538 for (
unsigned j=0;
j<tags[
i].n_data;
j++) {
544 const char** en =
CALLOC(xvars,
const char*);
545 const char** vn =
CALLOC(xvars,
const char*);
546 int* in =
CALLOC(xvars,
int);
547 int* n_data =
CALLOC(xvars,
int);
551 for (
unsigned i=0;
i<tags.size();
i++) {
552 if (tags[
i].
name[0] ==
'/')
554 if (tags[
i].n_data == 1) {
555 en[nvars] = event_name;
556 vn[nvars] = tags[
i].name;
558 n_data[nvars] = tags[
i].n_data;
561 for (
unsigned j=0;
j<tags[
i].n_data;
j++) {
562 en[nvars] = event_name;
563 vn[nvars] = tags[
i].name;
565 n_data[nvars] = tags[
i].n_data;
571 assert(nvars <= xvars);
574 time_t** tbuffer =
CALLOC(nvars, time_t*);
575 double** vbuffer =
CALLOC(nvars,
double*);
576 int* hs_status =
CALLOC(nvars,
int);
578 status =
mh->
hs_read(start_time, end_time, interval, nvars, en, vn, in,
n, tbuffer, vbuffer, hs_status);
581 printf(
"Cannot read history data, hs_read() error %d\n",
status);
590 for (
int i=0;
i<nvars;
i++) {
592 printf(
"No data for event \"%s\" variable \"%s\" index %d found in specified time range\n", en[
i], vn[
i], in[
i]);
595 printf(
"Cannot read event \"%s\" variable \"%s\" index %d, status %d\n", en[
i], vn[
i], in[
i], hs_status[
i]);
597 }
else if (
n[
i] != nread) {
598 printf(
"Number of entries for event \"%s\" variable \"%s\" index %d is %d instead of %d\n", en[
i], vn[
i], in[
i],
n[
i], nread);
607 printf(
"No data, nothing to print\n");
612 printf(
"Cannot print history data because of timestamp skew\n");
616 printf(
"Event \'%s\', %d variables, %d entries\n", event_name, nvars, nread);
620 for (
int i=0;
i<nvars;
i++) {
621 if (n_data[
i] == 1) {
623 printf(
"%s ", vn[
i]);
626 printf(
"%s[%d] ", vn[
i], in[
i]);
631 for (
int i = 0;
i < nread;
i++) {
633 printf(
"%d ", (
int)tbuffer[0][
i]);
636 time_t ltime = (time_t) tbuffer[0][
i];
638 ctime_r(<ime, ctimebuf);
639 sprintf(buf,
"%s", ctimebuf + 4);
640 char*
c = strchr(buf,
'\n');
647 for (
int j=0;
j<nvars;
j++) {
648 if (tbuffer[
j][
i] != tbuffer[0][
i]) {
649 printf(
"Cannot print history data because of timestamp skew\n");
653 printf(
"%.16g", vbuffer[
j][
i]);
659 for (
int i=0;
i<nvars;
i++) {
684 memset(&tms, 0,
sizeof(tms));
686 tms.tm_year = 10 * (t[0] -
'0') + (t[1] -
'0');
687 tms.tm_mon = 10 * (t[2] -
'0') + (t[3] -
'0') - 1;
688 tms.tm_mday = 10 * (t[4] -
'0') + (t[5] -
'0');
689 if (tms.tm_year < 90)
692 tms.tm_hour = 10 * (t[7] -
'0') + (t[8] -
'0');
693 tms.tm_min = 10 * (t[9] -
'0') + (t[10] -
'0');
695 tms.tm_sec = 10 * (t[11] -
'0') + (t[12] -
'0');
701 localtime_r(<ime, &tms);
703 isdst = tms.tm_isdst;
704 memset(&tms, 0,
sizeof(tms));
705 tms.tm_isdst = isdst;
707 tms.tm_year = 10 * (t[0] -
'0') + (t[1] -
'0');
708 tms.tm_mon = 10 * (t[2] -
'0') + (t[3] -
'0') - 1;
709 tms.tm_mday = 10 * (t[4] -
'0') + (t[5] -
'0');
710 if (tms.tm_year < 90)
713 tms.tm_hour = 10 * (t[7] -
'0') + (t[8] -
'0');
714 tms.tm_min = 10 * (t[9] -
'0') + (t[10] -
'0');
716 tms.tm_sec = 10 * (t[11] -
'0') + (t[12] -
'0');
726 printf(
"\nusage: mhist [-e Event Name] [-v Variable Name]\n");
727 printf(
" [-i Index] index of variables which are arrays\n");
728 printf(
" [-i Index1:Index2] index range of variables which are arrays (max 50)\n");
729 printf(
" [-t Interval] minimum interval in sec. between two displayed records\n");
730 printf(
" [-h Hours] display between some hours ago and now\n");
731 printf(
" [-d Days] display between some days ago and now\n");
732 printf(
" [-f File] specify history file explicitly\n");
733 printf(
" [-s Start date] specify start date YYMMDD[.HHMM[SS]]\n");
734 printf(
" [-p End date] specify end date YYMMDD[.HHMM[SS]]\n");
735 printf(
" [-l] list available events and variables\n");
736 printf(
" [-b] display time stamp in decimal format\n");
740int main(
int argc,
char *argv[])
743 DWORD start_time = 0;
752 std::string path_name;
754 BOOL do_hst_file =
false;
755 std::string event_name;
792 start_time = end_time - 3600;
800 for (
i = 1;
i < argc;
i++) {
801 if (argv[
i][0] ==
'-' && argv[
i][1] ==
'b')
803 else if (argv[
i][0] ==
'-' && argv[
i][1] ==
'l')
805 else if (argv[
i][0] ==
'-') {
807 printf(
"Error: command line switch value after \"%s\" is missing\n", argv[
i]);
809 }
else if (argv[
i+1][0] ==
'-') {
810 printf(
"Error: command line switch value after \"%s\" starts with a dash: %s\n", argv[
i], argv[
i+1]);
812 }
else if (strncmp(argv[
i],
"-e", 2) == 0) {
813 event_name = argv[++
i];
814 }
else if (strncmp(argv[
i],
"-v", 2) == 0) {
816 }
else if (strncmp(argv[
i],
"-i", 2) == 0) {
817 if ((column = strchr(argv[++
i],
':')) == NULL) {
818 index1 = atoi(argv[
i]);
822 index1 = atoi(argv[
i]);
823 index2 = atoi(column + 1);
825 }
else if (strncmp(argv[
i],
"-h", 2) == 0) {
826 start_time =
ss_time() - atoi(argv[++
i]) * 3600;
827 }
else if (strncmp(argv[
i],
"-d", 2) == 0) {
828 start_time =
ss_time() - atoi(argv[++
i]) * 3600 * 24;
829 }
else if (strncmp(argv[
i],
"-s", 2) == 0) {
831 }
else if (strncmp(argv[
i],
"-p", 2) == 0) {
833 }
else if (strncmp(argv[
i],
"-t", 2) == 0) {
834 interval = atoi(argv[++
i]);
835 }
else if (strncmp(argv[
i],
"-f", 2) == 0) {
836 path_name = argv[++
i];
840 printf(
"Error: unknown command line switch: %s\n", argv[
i]);
860 printf(
"hs_connect() error %d\n",
status);
875 else if (index2 == 0)
virtual int hs_read(time_t start_time, time_t end_time, time_t interval, int num_var, const char *const event_name[], const char *const tag_name[], const int var_index[], int num_entries[], time_t *time_buffer[], double *data_buffer[], int status[])=0
see hs_read(), returns HS_SUCCESS
virtual int hs_get_events(time_t time_from, std::vector< std::string > *pevents)=0
get list of events that exist(ed) at given time and later (value 0 means "return all events from begi...
virtual int hs_get_tags(const char *event_name, time_t time_from, std::vector< TAG > *ptags)=0
get list of history variables for given event (use event names returned by hs_get_events()) that exis...
INT cm_get_experiment_database(HNDLE *hDB, HNDLE *hKeyClient)
INT cm_connect_experiment1(const char *host_name, const char *default_exp_name, const char *client_name, void(*func)(char *), INT odb_size, DWORD watchdog_timeout)
INT cm_disconnect_experiment(void)
INT cm_get_environment(char *host_name, int host_name_size, char *exp_name, int exp_name_size)
static INT hs_enum_vars(DWORD ltime, DWORD event_id, char *var_name, DWORD *size, DWORD *var_n, DWORD *n_size)
static INT hs_define_event(DWORD event_id, const char *name, const TAG *tag, DWORD size)
static INT hs_count_vars(DWORD ltime, DWORD event_id, DWORD *count)
static INT hs_count_events(DWORD ltime, DWORD *count)
static INT hs_enum_events(DWORD ltime, char *event_name, DWORD *name_size, INT event_id[], DWORD *id_size)
static INT hs_dump(DWORD event_id, DWORD start_time, DWORD end_time, DWORD interval, BOOL binary_time)
static INT hs_write_event(DWORD event_id, const void *data, DWORD size)
time_t ss_mktime(struct tm *tms)
INT ss_sleep(INT millisec)
INT cm_msg(INT message_type, const char *filename, INT line, const char *routine, const char *format,...)
INT cm_set_msg_print(INT system_mask, INT user_mask, int(*func)(const char *))
int hs_get_history(HNDLE hDB, HNDLE hKey, int flags, int debug_flag, MidasHistoryInterface **mh)
BOOL debug
debug printouts
char host_name[HOST_NAME_LENGTH]
char expt_name[NAME_LENGTH]
static void display_all_hist(MidasHistoryInterface *mh, const char *event_name, time_t start_time, time_t end_time, time_t interval)
static void display_single_hist(MidasHistoryInterface *mh, const char *event_name, time_t start_time, time_t end_time, time_t interval, const char *var_name, int index)
static DWORD convert_time(char *t)
#define CALLOC(num, type)
static INT query_params(MidasHistoryInterface *mh, std::string *event_name, DWORD *start_time, DWORD *end_time, DWORD *interval, char *var_name, DWORD *var_type, INT *var_n_data, DWORD *index)
static INT display_vars(MidasHistoryInterface *mh, time_t t)
static INT hs_fdump(const char *file_name, DWORD id, BOOL binary_time)
static void display_range_hist(MidasHistoryInterface *mh, const char *event_name, time_t start_time, time_t end_time, time_t interval, const char *var_name, int index1, int index2)
MidasHistoryInterface * mh