Add .gitignore file
[s390-tools.git] / ziomon / ziomon_msg_tools.c
blobc83f6ca1b7acd16481d48433afb22b7875aa795a
1 /*
2 * FCP adapter trace facility
4 * Common utility functions to handle message structs
6 * Copyright IBM Corp. 2008
7 * Author(s): Stefan Raspl <raspl@linux.vnet.ibm.com>
8 */
10 #include <stdlib.h>
11 #include <string.h>
12 #include <assert.h>
14 #include "ziomon_msg_tools.h"
15 #include "ziomon_util.h"
16 #include "ziomon_zfcpdd.h"
17 #include "blkiomon.h"
20 extern const char *toolname;
25 void conv_blkiomon_v2_to_v3(struct message *msg) {
26 struct blkiomon_stat_v2 stat_v2 = *(struct blkiomon_stat_v2*)msg->data;
27 struct blkiomon_stat *stat = msg->data;
29 stat->device = stat_v2.device;
30 stat->size_r = stat_v2.size_r;
31 stat->size_w = stat_v2.size_w;
32 stat->d2c_r = stat_v2.d2c_r;
33 stat->d2c_w = stat_v2.d2c_w;
34 stat->thrput_r = stat_v2.thrput_r;
35 stat->thrput_w = stat_v2.thrput_w;
36 stat->bidir = stat_v2.bidir;
39 void conv_msg_data_to_BE(struct message *msg,
40 const struct file_header *hdr)
42 if (msg->type == hdr->msgid_utilization)
43 conv_overall_result_to_BE(msg->data);
44 else if (msg->type == hdr->msgid_ioerr)
45 conv_ioerr_data_to_BE(msg->data);
46 else if (msg->type == hdr->msgid_blkiomon)
47 blkiomon_conv_to_BE(msg->data);
48 else if (msg->type == hdr->msgid_zfcpdd)
49 conv_dstat_to_BE(msg->data);
50 else
51 fprintf(stderr, "%s: Unknown message encountered\n", toolname);
54 void conv_msg_data_from_BE(struct message *msg,
55 const struct file_header *hdr)
57 if (msg->type == hdr->msgid_utilization)
58 conv_overall_result_from_BE(msg->data);
59 else if (msg->type == hdr->msgid_ioerr)
60 conv_ioerr_data_from_BE(msg->data);
61 else if (msg->type == hdr->msgid_blkiomon)
62 blkiomon_conv_from_BE(msg->data);
63 else if (msg->type == hdr->msgid_zfcpdd)
64 conv_dstat_from_BE(msg->data);
65 else
66 fprintf(stderr,"%s: Unknown message encountered\n", toolname);
69 void conv_aggr_data_msg_data_to_BE(struct aggr_data *hdr)
71 __u64 i;
73 if (hdr->util_aggr)
74 conv_overall_result_to_BE(hdr->util_aggr->data);
75 if (hdr->ioerr_aggr)
76 conv_ioerr_data_to_BE(hdr->ioerr_aggr->data);
77 for (i=0; i<hdr->num_blkiomon; ++i)
78 blkiomon_conv_to_BE(hdr->blkio_aggr[i]->data);
79 for (i=0; i<hdr->num_zfcpdd; ++i)
80 conv_dstat_to_BE(hdr->zfcpdd_aggr[i]->data);
83 void conv_aggr_data_msg_data_from_BE(struct aggr_data *hdr)
85 __u64 i;
87 if (hdr->util_aggr)
88 conv_overall_result_from_BE(hdr->util_aggr->data);
89 if (hdr->ioerr_aggr)
90 conv_ioerr_data_from_BE(hdr->ioerr_aggr->data);
91 for (i=0; i<hdr->num_blkiomon; ++i)
92 blkiomon_conv_to_BE(hdr->blkio_aggr[i]->data);
93 for (i=0; i<hdr->num_zfcpdd; ++i)
94 conv_dstat_from_BE(hdr->zfcpdd_aggr[i]->data);
97 void copy_msg(struct message *src, struct message **tgt)
99 *tgt = malloc(sizeof(struct message));
100 (*tgt)->type = src->type;
101 (*tgt)->length = src->length;
102 (*tgt)->data = malloc(src->length);
103 memcpy((*tgt)->data, src->data, src->length);
106 time_t get_timestamp_from_BE_msg(const struct message *msg)
108 time_t timestamp = get_timestamp_from_msg(msg);
109 swap_64(timestamp);
111 return timestamp;
114 time_t get_timestamp_from_msg(const struct message *msg)
116 return *(__u64*)(msg->data);
119 static void aggregate_blkiomon(struct aggr_data *agg_data, struct message *msg)
121 __u64 i;
122 struct message **tmp;
123 struct blkiomon_stat *stat = msg->data;
125 for (i = 0; i < agg_data->num_blkiomon; ++i)
126 if ((((struct blkiomon_stat*)(agg_data->blkio_aggr[i]->data))->device)
127 == stat->device)
128 break;
129 if (i >= agg_data->num_blkiomon) {
130 /* messages for this device not aggregated yet
131 - add a new entry */
132 tmp = agg_data->blkio_aggr;
133 ++(agg_data->num_blkiomon);
134 agg_data->blkio_aggr =
135 malloc(agg_data->num_blkiomon * sizeof(struct message*));
136 for (i = 0; i < agg_data->num_blkiomon - 1; ++i)
137 agg_data->blkio_aggr[i] = tmp[i];
138 copy_msg(msg, &agg_data->blkio_aggr[i]);
139 free(tmp);
141 else
142 blkiomon_stat_merge(agg_data->blkio_aggr[i]->data, msg->data);
146 static void aggregate_zfcpdd(struct aggr_data *agg_data, struct message *msg)
148 __u64 i;
149 struct message **tmp;
150 struct zfcpdd_dstat *stat = msg->data;
152 for (i = 0; i < agg_data->num_zfcpdd; ++i)
153 if ((((struct zfcpdd_dstat*)(agg_data->zfcpdd_aggr[i]->data))->device)
154 == stat->device)
155 break;
156 if (i >= agg_data->num_zfcpdd) {
157 /* messages for this device not aggregated yet
158 - add a new entry */
159 tmp = agg_data->zfcpdd_aggr;
160 ++(agg_data->num_zfcpdd);
161 agg_data->zfcpdd_aggr =
162 malloc(agg_data->num_zfcpdd * sizeof(struct message*));
163 for (i = 0; i < agg_data->num_zfcpdd - 1; ++i)
164 agg_data->zfcpdd_aggr[i] = tmp[i];
165 copy_msg(msg, &agg_data->zfcpdd_aggr[i]);
166 free(tmp);
168 else
169 aggregate_dstat(msg->data, agg_data->zfcpdd_aggr[i]->data);
173 int add_to_agg(struct aggr_data *agg_data, struct message *msg,
174 const struct file_header *f_hdr)
176 assert(agg_data->magic == DATA_MGR_MAGIC_AGGR);
178 conv_msg_data_from_BE(msg, f_hdr);
180 if (msg->type == f_hdr->msgid_utilization) {
181 if (agg_data->util_aggr)
182 aggregate_utilization_data(msg->data,
183 agg_data->util_aggr->data);
184 else
185 copy_msg(msg, &agg_data->util_aggr);
186 } else if (msg->type == f_hdr->msgid_ioerr) {
187 if (agg_data->ioerr_aggr)
188 aggregate_ioerr_data(msg->data,
189 agg_data->ioerr_aggr->data);
190 else
191 copy_msg(msg, &agg_data->ioerr_aggr);
192 } else if (msg->type == f_hdr->msgid_blkiomon)
193 aggregate_blkiomon(agg_data, msg);
194 else if (msg->type == f_hdr->msgid_zfcpdd)
195 aggregate_zfcpdd(agg_data, msg);
196 else {
197 fprintf(stderr, "%s: Unknow msg id: %d,"
198 " discarding\n", toolname, msg->type);
199 return -1;
201 agg_data->end_time = get_timestamp_from_msg(msg);
202 if (agg_data->begin_time == 0)
203 agg_data->begin_time = agg_data->end_time;
205 return 0;