summaryrefslogtreecommitdiff
path: root/src/bin/evas/evas_cserve2_usage.c
blob: 449c9fdd445ba4e34105614b1a9885b76d464276 (plain) (blame)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <fcntl.h>

#include <Eina.h>

#include "evas_cs2.h"

static int socketfd = -1;
static unsigned int _rid_count = 1;
static int _evas_cserve2_usage_log_dom = -1;

#ifndef UNIX_PATH_MAX
#define UNIX_PATH_MAX sizeof(((struct sockaddr_un *)NULL)->sun_path)
#endif

#ifdef ERR
#undef ERR
#endif
#define ERR(...) EINA_LOG_DOM_ERR(_evas_cserve2_usage_log_dom, __VA_ARGS__)
#ifdef DBG
#undef DBG
#endif
#define DBG(...) EINA_LOG_DOM_DBG(_evas_cserve2_usage_log_dom, __VA_ARGS__)
#ifdef WRN
#undef WRN
#endif
#define WRN(...) EINA_LOG_DOM_WARN(_evas_cserve2_usage_log_dom, __VA_ARGS__)
#ifdef INF
#undef INF
#endif
#define INF(...) EINA_LOG_DOM_INFO(_evas_cserve2_usage_log_dom, __VA_ARGS__)

static void
_socket_path_set(char *path)
{
   char *env;
   char buf[UNIX_PATH_MAX];

   env = getenv("EVAS_CSERVE2_SOCKET");
   if (env && env[0])
     {
        eina_strlcpy(path, env, UNIX_PATH_MAX);
        return;
     }

   snprintf(buf, sizeof(buf), "/tmp/.evas-cserve2-%x.socket", (int)getuid());
   /* FIXME: check we can actually create this socket */
   strcpy(path, buf);
}

static Eina_Bool
_server_connect(void)
{
   int s, len;
   struct sockaddr_un remote;

   if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
     {
        ERR("socket");
        return EINA_FALSE;
     }

   remote.sun_family = AF_UNIX;
   _socket_path_set(remote.sun_path);
   len = strlen(remote.sun_path) + sizeof(remote.sun_family);
   if (connect(s, (struct sockaddr *)&remote, len) == -1)
     {
        close(s);
        ERR("connect");
        return EINA_FALSE;
     }

   if (fcntl(s, F_SETFL, O_NONBLOCK) < 0) ERR("can't set non-blocking fd");

   socketfd = s;

   DBG("connected to cserve2 server.");
   return EINA_TRUE;
}

static void
_server_disconnect(void)
{
   close(socketfd);
   socketfd = -1;
}

static Eina_Bool
_server_send(const void *data, int size)
{
   int sent = 0;
   ssize_t ret;
   const char *msg = data;

   while (sent < size)
     {
        ret = send(socketfd, msg + sent, size - sent, MSG_NOSIGNAL);
        if (ret < 0)
          {
             if ((errno == EAGAIN) || (errno == EINTR))
               continue;
             return EINA_FALSE;
          }
        sent += ret;
     }

   return EINA_TRUE;
}

static int sr_size = 0;
static int sr_got = 0;
static char *sr_buf = NULL;

static void *
_server_read(int *size)
{
   int n;
   void *ret;

   if (sr_size)
     goto get_data;

   n = recv(socketfd, &sr_size, sizeof(sr_size), 0);
   if (n < 0)
     return NULL;

   // We don't expect large messages
   if (sr_size < 0 || sr_size > 0x10000)
     return NULL;

   sr_buf = malloc(sr_size);

get_data:
   n = recv(socketfd, sr_buf + sr_got, sr_size - sr_got, 0);
   if (n < 0)
     return NULL;

   sr_got += n;
   if (sr_got < sr_size)
     return NULL;

   *size = sr_size;
   sr_size = 0;
   sr_got = 0;
   ret = sr_buf;
   sr_buf = NULL;

   return ret;
}

static void
_usage_msg_send(void)
{
   Msg_Base msg;
   int size;

   memset(&msg, 0, sizeof(msg));
   msg.type = CSERVE2_STATS;
   msg.rid = _rid_count++;

   size = sizeof(msg);

   if (!_server_send(&size, sizeof(size)))
     {
        ERR("Could not send usage msg size to server.");
        return;
     }
   if (!_server_send(&msg, size))
     {
        ERR("Could not send usage msg body to server.");
        return;
     }
}

static void
_shared_index_print(Msg_Base *data, size_t size)
{
   Msg_Index_List *msg = (Msg_Index_List *) data;

   if (size < sizeof(*msg) || msg->base.type != CSERVE2_INDEX_LIST)
     {
        ERR("Invalid message received from server. "
            "Something went wrong.");
        return;
     }

   printf("Printing shared indexes status.\n");
   printf("===============================\n\n");
   printf("Generation ID:        %-4d\n", msg->generation_id);
   printf("Strings entries path: %s\n", msg->strings_entries_path);
   printf("Strings index path:   %s\n", msg->strings_index_path);
   printf("Files index path:     %s\n", msg->files_index_path);
   printf("Images index path:    %s\n", msg->images_index_path);
   printf("Fonts index path:     %s\n", msg->fonts_index_path);
   printf("\n\n\n");
}

static void
_usage_msg_read(void)
{
   Msg_Stats *stats = NULL;
   int size;

   printf("Requesting server statistics.\n\n");

   while (!stats)
     {
        Msg_Base *msg = _server_read(&size);
        if (!msg) continue;
        switch (msg->type)
          {
           case CSERVE2_INDEX_LIST:
             _shared_index_print(msg, size);
             break;
           case CSERVE2_STATS:
             stats = (Msg_Stats *) msg;
             break;
           default:
             ERR("Invalid message received from server. "
                 "Something went badly wrong.");
             return;
          }
     }

   printf("Printing server usage.\n");
   printf("======================\n\n");
   printf("\nImage Usage Statistics:\n");
   printf("----------------------\n\n");
   printf("Image headers usage: %d bytes\n", stats->images.files_size);
   printf("Image data requested: %d kbytes\n", stats->images.requested_size / 1024);
   printf("Image data usage: %d kbytes\n", stats->images.images_size / 1024);
   printf("Image data unused: %d kbytes\n", stats->images.unused_size / 1024);
   printf("Image headers load time: %dus\n", stats->images.files_load_time);
   printf("Image headers saved time: %dus\n", stats->images.files_saved_time);
   printf("Image data load time: %dus\n", stats->images.images_load_time);
   printf("Image data saved time: %dus\n", stats->images.images_saved_time);
   printf("\nFont Usage Statistics:\n");
   printf("----------------------\n\n");
   printf("Requested usage: %d bytes\n", stats->fonts.requested_size);
   printf("Real usage: %d bytes\n", stats->fonts.real_size);
   printf("Unused size: %d bytes\n", stats->fonts.unused_size);
   printf("Fonts load time: %dus\n", stats->fonts.fonts_load_time);
   printf("Fonts used load time: %dus\n", stats->fonts.fonts_used_load_time);
   printf("Fonts used saved time: %dus\n", stats->fonts.fonts_used_saved_time);
   printf("Glyphs load time: %dus\n", stats->fonts.glyphs_load_time);
   printf("Glyphs render time: %dus\n", stats->fonts.glyphs_render_time);
   printf("Glyphs saved time: %dus\n", stats->fonts.glyphs_saved_time);
   printf("Glyphs request time: %dus\n", stats->fonts.glyphs_request_time);
   printf("Glyphs slave time: %dus\n", stats->fonts.glyphs_slave_time);

   printf("\n");
}

int
main(void)
{
   eina_init();
   _evas_cserve2_usage_log_dom = eina_log_domain_register
      ("evas_cserve2_usage", EINA_COLOR_BLUE);
   if (!_server_connect())
     {
        ERR("Could not connect to server.");
        return -1;
     }
   _usage_msg_send();
   _usage_msg_read();
   _server_disconnect();
   eina_shutdown();
   return 0;
}