Debian Codegraph @

c/tools/msgr-common.c

raw
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 *
19 */
20
21 #include "msgr-common.h"
22 #include <pncompat/misc_funcs.inc>
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <assert.h>
28
29 void msgr_die(const char *file, int line, const char *message)
30 {
31 fprintf(stderr, "%s:%i: %s\n", file, line, message);
32 exit(1);
33 }
34
35 //sigh - would be nice if proton exported pn_strdup()
36 char *msgr_strdup( const char *src )
37 {
38 char *r = NULL;
39 if (src) {
40 r = (char *) malloc(sizeof(char) * (strlen(src) + 1));
41 if (r) strcpy(r,src);
42 }
43 return r;
44 }
45
46
47 pn_timestamp_t msgr_now()
48 {
49 // from "pncompat/misc_funcs.inc"
50 return time_now();
51 }
52
53 void addresses_init( Addresses_t *a )
54 {
55 a->size = 10; // whatever
56 a->count = 0;
57 a->addresses = (const char **) calloc( a->size, sizeof(const char *));
58 check(a->addresses, "malloc failure");
59 }
60
61 void addresses_free( Addresses_t *a )
62 {
63 if (a->addresses) {
64 int i;
65 for (i = 0; i < a->count; i++)
66 if (a->addresses[i]) free( (void *)a->addresses[i] );
67 free( (void *) a->addresses );
68 a->addresses = NULL;
69 }
70 }
71
72 void addresses_add( Addresses_t *a, const char *addr )
73 {
74 if (a->count == a->size) {
75 a->size += 10;
76 a->addresses = (const char **) realloc( a->addresses,
77 a->size * sizeof(const char *) );
78 check( a->addresses, "malloc failure" );
79 int i;
80 for (i = a->count; i < a->size; i++)
81 a->addresses[i] = NULL;
82 }
83 a->addresses[a->count] = msgr_strdup(addr);
84 check( a->addresses[a->count], "malloc failure" );
85 a->count++;
86 }
87
88 // merge a comma-separated list of addresses
89 void addresses_merge( Addresses_t *a, const char *list )
90 {
91 char *const l = msgr_strdup(list);
92 check( l, "malloc failure" );
93 char *addr = l;
94 while (addr && *addr) {
95 char *comma = strchr( addr, ',' );
96 if (comma) {
97 *comma++ = '\0';
98 }
99 addresses_add( a, addr );
100 addr = comma;
101 }
102 free(l);
103 }
104
105
106 void statistics_start( Statistics_t *s )
107 {
108 s->latency_samples = 0;
109 s->latency_total = s->latency_min = s->latency_max = 0.0;
110 s->start = msgr_now();
111 }
112
113 void statistics_msg_received( Statistics_t *s, pn_message_t *message )
114 {
115 pn_timestamp_t ts = pn_message_get_creation_time( message );
116 if (ts) {
117 double l = (double)(msgr_now() - ts);
118 if (l > 0) {
119 s->latency_total += l;
120 if (++s->latency_samples == 1) {
121 s->latency_min = s->latency_max = l;
122 } else {
123 if (s->latency_min > l)
124 s->latency_min = l;
125 if (s->latency_max < l)
126 s->latency_max = l;
127 }
128 }
129 }
130 }
131
132 void statistics_report( Statistics_t *s, uint64_t sent, uint64_t received )
133 {
134 pn_timestamp_t end = msgr_now() - s->start;
135 double secs = end/(double)1000.0;
136
137 fprintf(stdout, "Messages sent: %" PRIu64 " recv: %" PRIu64 "\n", sent, received );
138 fprintf(stdout, "Total time: %f sec\n", secs );
139 fprintf(stdout, "Throughput: %f msgs/sec\n", (secs != 0.0) ? (double)sent/secs : 0);
140 fprintf(stdout, "Latency (sec): %f min %f max %f avg\n",
141 s->latency_min/1000.0, s->latency_max/1000.0,
142 (s->latency_samples) ? (s->latency_total/s->latency_samples)/1000.0 : 0);
143 }
144
145 void parse_password( const char *input, char **password )
146 {
147 if (strncmp( input, "pass:", 5 ) == 0) {
148 // password provided on command line (not secure, shows up in 'ps')
149 *password = msgr_strdup( input + 5 );
150 } else { // input assumed to be file containing password
151 FILE *f = fopen( input, "r" );
152 check( f, "Cannot open password file\n" );
153 *password = (char *)malloc(256); // 256 should be enough for anybody!
154 check( *password, "malloc failure" );
155 int rc = fscanf( f, "%255s", *password );
156 check( rc == 1, "Cannot read password from file\n" );
157 fclose(f);
158 }
159 }
160
161 static int dolog = 0;
162 void enable_logging()
163 {
164 dolog = 1;
165 }
166
167 void LOG( const char *fmt, ... )
168 {
169 if (dolog) {
170 va_list ap;
171 va_start(ap, fmt);
172 vfprintf( stdout, fmt, ap );
173 va_end(ap);
174 }
175 }