1 /*****************************************************************************
2 * This file is part of gfxprim library. *
4 * Gfxprim is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU Lesser General Public *
6 * License as published by the Free Software Foundation; either *
7 * version 2.1 of the License, or (at your option) any later version. *
9 * Gfxprim is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
12 * Lesser General Public License for more details. *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with gfxprim; if not, write to the Free Software *
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17 * Boston, MA 02110-1301 USA *
19 * Copyright (C) 2009-2012 Cyril Hrubis <metan@ucw.cz> *
21 *****************************************************************************/
23 #include <sys/utsname.h>
29 #include "tst_preload.h"
30 #include "tst_timespec.h"
33 static const char *ret_to_str(enum tst_ret ret
)
43 return "Internal Error";
45 return "Segmentation Fault";
51 return "FP Exception";
63 static int append_msg_json(struct tst_job
*job
, FILE *f
)
67 fprintf(f
, "\t\t\t\"Test Reports\": [\n");
69 for (msg
= job
->store
.first
; msg
!= NULL
; msg
= msg
->next
) {
70 fprintf(f
, "\t\t\t\t\"%s\"", msg
->msg
);
72 if (msg
->next
!= NULL
)
76 fprintf(f
, "\n\t\t\t],\n");
81 static int append_malloc_stats_json(struct tst_job
*job
, FILE *f
)
83 fprintf(f
, "\t\t\t\"Malloc Stats\": {\n");
84 fprintf(f
, "\t\t\t\t\"Total Size\": %zi,\n",
85 job
->malloc_stats
.total_size
);
86 fprintf(f
, "\t\t\t\t\"Total Chunks\": %u,\n",
87 job
->malloc_stats
.total_chunks
);
88 fprintf(f
, "\t\t\t\t\"Max Size\": %zi,\n",
89 job
->malloc_stats
.max_size
);
90 fprintf(f
, "\t\t\t\t\"Max Chunks\": %u,\n",
91 job
->malloc_stats
.max_chunks
);
92 fprintf(f
, "\t\t\t\t\"Lost Size\": %zi,\n",
93 job
->malloc_stats
.lost_size
);
94 fprintf(f
, "\t\t\t\t\"Lost Chunks\": %u\n",
95 job
->malloc_stats
.lost_chunks
);
96 fprintf(f
, "\t\t\t},\n");
101 static void append_benchmark_json(struct tst_job
*job
, FILE *f
)
103 fprintf(f
, "\t\t\t\"Benchmark\": {\n");
105 fprintf(f
, "\t\t\t\t\"Time Mean\": %i.%09i,\n",
106 (int)job
->bench_mean
.tv_sec
,
107 (int)job
->bench_mean
.tv_nsec
);
109 fprintf(f
, "\t\t\t\t\"Time Variance\": %i.%09i,\n",
110 (int)job
->bench_var
.tv_sec
,
111 (int)job
->bench_var
.tv_nsec
);
113 fprintf(f
, "\t\t\t\t\"Iterations\": %i\n", job
->bench_iter
);
115 fprintf(f
, "\t\t\t},\n");
118 static int hack_json_start
= 0;
120 int tst_log_append(struct tst_job
*job
, FILE *f
)
127 fprintf(f
, "\t\t{\n");
128 fprintf(f
, "\t\t\t\"Test Name\": \"%s\",\n", job
->test
->name
);
129 fprintf(f
, "\t\t\t\"Test Result\": \"%s\",\n", ret_to_str(job
->result
));
131 /* Append any test reports */
132 append_msg_json(job
, f
);
134 /* If calculated include malloc report */
135 if (job
->test
->flags
& TST_CHECK_MALLOC
)
136 append_malloc_stats_json(job
, f
);
138 /* If benchmark data were created */
140 append_benchmark_json(job
, f
);
142 /* Time statistics */
145 timespec_diff(&sec
, &nsec
, &job
->start_time
, &job
->stop_time
);
147 fprintf(f
, "\t\t\t\"CPU Time\": %i.%09i,\n",
148 (int)job
->cpu_time
.tv_sec
, (int)job
->cpu_time
.tv_nsec
);
150 fprintf(f
, "\t\t\t\"Run Time\": %i.%09i\n", sec
, nsec
);
157 static void write_system_info_json(FILE *f
)
163 fprintf(f
, "\t\"System Info\": {\n");
165 fprintf(f
, "\t\t\"OS\": \"%s\",\n", buf
.sysname
);
166 fprintf(f
, "\t\t\"Hostname\": \"%s\",\n", buf
.nodename
);
167 fprintf(f
, "\t\t\"Release\": \"%s\",\n", buf
.release
);
169 /* CPU related info */
170 fprintf(f
, "\t\t\"CPU\": {");
172 /* lscpu is part of reasonably new util-linux */
173 FILE *cmd
= popen("lscpu 2> /dev/null", "r");
176 char id
[256], val
[1024];
179 while (fscanf(cmd
, "%[^:]%*c %[^\n]\n", id
, val
) == 2) {
180 fprintf(f
, "%s\t\t\t\"%s\": \"%s\"", del
, id
, val
);
188 fprintf(f
, "\t\t}\n");
190 fprintf(f
, "\t},\n");
193 FILE *tst_log_open(const struct tst_suite
*suite
, const char *path
)
197 f
= fopen(path
, "w");
203 fprintf(f
, "\t\"Suite Name\": \"%s\",\n", suite
->suite_name
);
204 write_system_info_json(f
);
205 fprintf(f
, "\t\"Test Results\": [\n");
212 int tst_log_close(FILE *f
)
214 fprintf(f
, "\n\t]\n}\n");