trace_view.c 5.16 KB
Newer Older
Jeroen Vreeken's avatar
Jeroen Vreeken committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*
	Copyright Jeroen Vreeken (pe1rxq@amsat.org), 2007
	Copyright Stichting C.A. Muller Radioastronomiestation, 2007

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.

 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <math.h>
#include <time.h>

30
#include <trace/trace.h>
Jeroen Vreeken's avatar
Jeroen Vreeken committed
31
32
33

time_t starttime;

34
#define VAL_BUFLEN (60 * 5)
Jeroen Vreeken's avatar
Jeroen Vreeken committed
35
36
37
FILE *fdplot = NULL;
int doplot = 0;

38
39
struct trace_view {
	struct trace *trace;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
40
41
	char *tmpfilename;
	FILE *fdtmp;
42
43
	struct trace_value value[VAL_BUFLEN];
	int bufpos;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
44
45
};

46
47
struct trace_view *traces;

Jeroen Vreeken's avatar
Jeroen Vreeken committed
48

49
void plot_values(struct trace_view *traces, int nr_traces)
Jeroen Vreeken's avatar
Jeroen Vreeken committed
50
51
{
	int i;
52
53
54
	int bufpos = (traces[0].bufpos+VAL_BUFLEN-1) % VAL_BUFLEN;
	long long now = traces[0].value[bufpos].t.tv_sec;
	long nown = traces[0].value[bufpos].t.tv_nsec;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
55
	
56
	fprintf(fdplot, "plot ");
Jeroen Vreeken's avatar
Jeroen Vreeken committed
57

58
59
60
61
62
63
64
65
	for(i = 0; i < nr_traces; i++) {
		if (i > 0)
			fprintf(fdplot, ", ");
		fprintf(fdplot, "'%s' using ($1-%lld.%09ld):($2) title '%s [%s]' with lines",
		    traces[i].tmpfilename,
		    now, nown,
		    traces[i].trace->name, 
		    "");
Jeroen Vreeken's avatar
Jeroen Vreeken committed
66
	}
67
68
	fprintf(fdplot, "\n");
	fflush(NULL);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
69

70
	return;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
71
72
}

73
74
static void print_value(FILE *fd,
    struct trace *trace, struct trace_value *value)
Jeroen Vreeken's avatar
Jeroen Vreeken committed
75
{
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
	switch (trace->type) {
		case TRACE_VALUE_TYPE_FLOAT:
			fprintf(fd, "%ld.%09ld %e\n", 
			value->t.tv_sec, value->t.tv_nsec, value->value.f);
			break;
		case TRACE_VALUE_TYPE_BOOL:
			fprintf(fd, "%ld.%09ld %d\n", 
			value->t.tv_sec, value->t.tv_nsec, value->value.b);
			break;
		case TRACE_VALUE_TYPE_UINT8:
			fprintf(fd, "%ld.%09ld %u\n", 
			value->t.tv_sec, value->t.tv_nsec, value->value.u8);
			break;
		case TRACE_VALUE_TYPE_UINT16:
			fprintf(fd, "%ld.%09ld %u\n", 
			value->t.tv_sec, value->t.tv_nsec, value->value.u16);
			break;
		case TRACE_VALUE_TYPE_UINT32:
			fprintf(fd, "%ld.%09ld %u\n", 
			value->t.tv_sec, value->t.tv_nsec, value->value.u32);
			break;
		case TRACE_VALUE_TYPE_SINT8:
			fprintf(fd, "%ld.%09ld %d\n", 
			value->t.tv_sec, value->t.tv_nsec, value->value.s8);
			break;
		case TRACE_VALUE_TYPE_SINT16:
			fprintf(fd, "%ld.%09ld %d\n", 
			value->t.tv_sec, value->t.tv_nsec, value->value.s16);
			break;
		case TRACE_VALUE_TYPE_SINT32:
			fprintf(fd, "%ld.%09ld %d\n", 
			value->t.tv_sec, value->t.tv_nsec, value->value.s32);
			break;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
109
110
111
	}
}

112
113
static void handler_value(struct trace *trace,
    struct trace_value *value)
Jeroen Vreeken's avatar
Jeroen Vreeken committed
114
{
115
	struct trace_view *view;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
116
117
	int i;
	
118
119
120
	view = trace->private;
	
	view->value[view->bufpos] = *value;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
121

122
123
124
125
126
127
	freopen(NULL, "w", view->fdtmp);
	for (i = view->bufpos + 1; i < VAL_BUFLEN; i++) {
		if (!view->value[i].t.tv_sec)
			continue;

		print_value(view->fdtmp, trace, &view->value[i]);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
128
	}
129
130
131
	for (i = 0; i <= view->bufpos; i++) {
		if (!view->value[i].t.tv_sec)
			continue;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
132

133
134
135
136
137
138
139
140
141
		print_value(view->fdtmp, trace, &view->value[i]);
	}
	fflush(view->fdtmp);


	view->bufpos++;
	view->bufpos %= VAL_BUFLEN;
	
	doplot++;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
142
143
144
145
146
147
148
149
}

int main(int argc, char **argv)
{
        int i, ret = 0;

	starttime = time(NULL);

150
	traces = calloc(sizeof(struct trace_view), argc - 1);
Jeroen Vreeken's avatar
Jeroen Vreeken committed
151
	for (i = 0; i < argc - 1; i++) {
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
		char *tracename = argv[i+1];
		struct trace_pkt *pkt;
		struct timespec t_int;
		int fdmkstemp;
		
		printf("%s\n", tracename);
		traces[i].trace = trace_open("localhost", 10000);
		if (!traces[i].trace) {
			printf("open trace failed\n");
			return 1;
		}
		traces[i].trace->private = &traces[i];
		trace_name_set(traces[i].trace, tracename);

		traces[i].tmpfilename = malloc(100);
		sprintf(traces[i].tmpfilename, "/tmp/trace_view_XXXXXX");
		fdmkstemp = mkstemp(traces[i].tmpfilename);
	
		printf("Going to use %s temp file for '%s' data\n",
		    traces[i].tmpfilename, tracename);
		traces[i].fdtmp = fdopen(fdmkstemp, "w");
		if (traces[i].fdtmp == NULL) {
			perror("fopen() failed");
			return 1;
		}

		t_int.tv_sec = 0;
		t_int.tv_nsec = 200*1000*1000;

		pkt = trace_packet_new();
		trace_packet_interval_set(pkt, 
		    &t_int, TRACE_INTERVAL_TYPE_INTERVAL);
		trace_packet_write(traces[i].trace, pkt);
		trace_packet_put(pkt);

		traces[i].trace->handler_value = handler_value;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
188
189
190
191
192
193
194
195
196
197
198
	}
	
	fdplot = popen("gnuplot -noraise", "w");
	if (fdplot == NULL) {
		perror("popen() failed");
		return 1;
	}
	
	fprintf(fdplot, "set grid\n");

	do {
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
		fd_set fdrx;
		
		int high = 0;
		
		FD_ZERO(&fdrx);
	
		for (i = 0; i < argc - 1; i++)
			trace_fd_set(traces[i].trace, &fdrx, &high);
		
		select(high+1, &fdrx, NULL, NULL, NULL);

		for (i = 0; i < argc - 1; i++)
			trace_handle(traces[i].trace, &fdrx);

		if (doplot >= argc - 1) {
			plot_values(traces, argc - 1);
			doplot = 0;
Jeroen Vreeken's avatar
Jeroen Vreeken committed
216
217
218
219
220
221
222
223
		}
	} while (ret == 0);

	for (i = 0; i < argc - 1; i++)
		unlink(traces[i].tmpfilename);

	return 0;
}