gnunet-service-statistics.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2009, 2010, 2012 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file statistics/gnunet-service-statistics.c
  19. * @brief program that tracks statistics
  20. * @author Christian Grothoff
  21. */
  22. #include "platform.h"
  23. #include "gnunet_bio_lib.h"
  24. #include "gnunet_container_lib.h"
  25. #include "gnunet_disk_lib.h"
  26. #include "gnunet_getopt_lib.h"
  27. #include "gnunet_protocols.h"
  28. #include "gnunet_service_lib.h"
  29. #include "gnunet_statistics_service.h"
  30. #include "gnunet_strings_lib.h"
  31. #include "gnunet_time_lib.h"
  32. #include "statistics.h"
  33. /**
  34. * Watch entry.
  35. */
  36. struct WatchEntry
  37. {
  38. /**
  39. * Watch entries are kept in a linked list.
  40. */
  41. struct WatchEntry *next;
  42. /**
  43. * Watch entries are kept in a linked list.
  44. */
  45. struct WatchEntry *prev;
  46. /**
  47. * For which client is this watch entry?
  48. */
  49. struct GNUNET_SERVER_Client *client;
  50. /**
  51. * Last value we communicated to the client for this watch entry.
  52. */
  53. uint64_t last_value;
  54. /**
  55. * Unique watch number for this client and this watched value.
  56. */
  57. uint32_t wid;
  58. /**
  59. * Is last_value valid
  60. * GNUNET_NO : last_value is n/a, GNUNET_YES: last_value is valid
  61. */
  62. int last_value_set;
  63. };
  64. /**
  65. * Client entry.
  66. */
  67. struct ClientEntry
  68. {
  69. /**
  70. * Clients are kept in a linked list.
  71. */
  72. struct ClientEntry *next;
  73. /**
  74. * Clients are kept in a linked list.
  75. */
  76. struct ClientEntry *prev;
  77. /**
  78. * Corresponding server handle.
  79. */
  80. struct GNUNET_SERVER_Client *client;
  81. /**
  82. * Maximum watch ID used by this client so far.
  83. */
  84. uint32_t max_wid;
  85. };
  86. /**
  87. * Entry in the statistics list.
  88. */
  89. struct StatsEntry
  90. {
  91. /**
  92. * This is a linked list.
  93. */
  94. struct StatsEntry *next;
  95. /**
  96. * Name of the service, points into the
  97. * middle of msg.
  98. */
  99. const char *service;
  100. /**
  101. * Name for the value, points into
  102. * the middle of msg.
  103. */
  104. const char *name;
  105. /**
  106. * Message that can be used to set this value,
  107. * stored at the end of the memory used by
  108. * this struct.
  109. */
  110. struct GNUNET_STATISTICS_SetMessage *msg;
  111. /**
  112. * Watch context for changes to this
  113. * value, or NULL for none.
  114. */
  115. struct WatchEntry *we_head;
  116. /**
  117. * Watch context for changes to this
  118. * value, or NULL for none.
  119. */
  120. struct WatchEntry *we_tail;
  121. /**
  122. * Our value.
  123. */
  124. uint64_t value;
  125. /**
  126. * Unique ID.
  127. */
  128. uint32_t uid;
  129. /**
  130. * Is this value persistent?
  131. */
  132. int persistent;
  133. /**
  134. * Is this value set?
  135. * GNUNET_NO : value is n/a, GNUNET_YES: value is valid
  136. */
  137. int set;
  138. };
  139. /**
  140. * Our configuration.
  141. */
  142. static const struct GNUNET_CONFIGURATION_Handle *cfg;
  143. /**
  144. * Linked list of our active statistics.
  145. */
  146. static struct StatsEntry *start;
  147. /**
  148. * Head of linked list of connected clients.
  149. */
  150. static struct ClientEntry *client_head;
  151. /**
  152. * Tail of linked list of connected clients.
  153. */
  154. static struct ClientEntry *client_tail;
  155. /**
  156. * Handle to our server.
  157. */
  158. static struct GNUNET_SERVER_Handle *srv;
  159. /**
  160. * Our notification context.
  161. */
  162. static struct GNUNET_SERVER_NotificationContext *nc;
  163. /**
  164. * Counter used to generate unique values.
  165. */
  166. static uint32_t uidgen;
  167. /**
  168. * Set to YES if we are shutting down as soon as possible.
  169. */
  170. static int in_shutdown;
  171. /**
  172. * Inject a message to our server with a client of 'NULL'.
  173. *
  174. * @param cls the 'struct GNUNET_SERVER_Handle'
  175. * @param client unused
  176. * @param msg message to inject
  177. */
  178. static int
  179. inject_message (void *cls, void *client, const struct GNUNET_MessageHeader *msg)
  180. {
  181. struct GNUNET_SERVER_Handle *server = cls;
  182. GNUNET_break (GNUNET_OK == GNUNET_SERVER_inject (server, NULL, msg));
  183. return GNUNET_OK;
  184. }
  185. /**
  186. * Load persistent values from disk. Disk format is
  187. * exactly the same format that we also use for
  188. * setting the values over the network.
  189. *
  190. * @param server handle to the server context
  191. */
  192. static void
  193. load (struct GNUNET_SERVER_Handle *server)
  194. {
  195. char *fn;
  196. struct GNUNET_BIO_ReadHandle *rh;
  197. uint64_t fsize;
  198. char *buf;
  199. struct GNUNET_SERVER_MessageStreamTokenizer *mst;
  200. char *emsg;
  201. if (GNUNET_OK !=
  202. GNUNET_CONFIGURATION_get_value_filename (cfg,
  203. "STATISTICS",
  204. "DATABASE",
  205. &fn))
  206. {
  207. GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
  208. "STATISTICS",
  209. "DATABASE");
  210. return;
  211. }
  212. if ( (GNUNET_OK !=
  213. GNUNET_DISK_file_size (fn, &fsize, GNUNET_NO, GNUNET_YES)) ||
  214. (0 == fsize) )
  215. {
  216. GNUNET_free (fn);
  217. return;
  218. }
  219. buf = GNUNET_malloc (fsize);
  220. rh = GNUNET_BIO_read_open (fn);
  221. if (!rh)
  222. {
  223. GNUNET_free (buf);
  224. GNUNET_free (fn);
  225. return;
  226. }
  227. if (GNUNET_OK != GNUNET_BIO_read (rh, fn, buf, fsize))
  228. {
  229. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "read", fn);
  230. GNUNET_break (GNUNET_OK == GNUNET_BIO_read_close (rh, &emsg));
  231. GNUNET_free (buf);
  232. GNUNET_free_non_null (emsg);
  233. GNUNET_free (fn);
  234. return;
  235. }
  236. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  237. _("Loading %llu bytes of statistics from `%s'\n"),
  238. fsize, fn);
  239. mst = GNUNET_SERVER_mst_create (&inject_message, server);
  240. GNUNET_break (GNUNET_OK ==
  241. GNUNET_SERVER_mst_receive (mst, NULL, buf, fsize,
  242. GNUNET_YES, GNUNET_NO));
  243. GNUNET_SERVER_mst_destroy (mst);
  244. GNUNET_free (buf);
  245. GNUNET_break (GNUNET_OK == GNUNET_BIO_read_close (rh, &emsg));
  246. GNUNET_free_non_null (emsg);
  247. GNUNET_free (fn);
  248. }
  249. /**
  250. * Write persistent statistics to disk.
  251. */
  252. static void
  253. save ()
  254. {
  255. struct StatsEntry *pos;
  256. char *fn;
  257. struct GNUNET_BIO_WriteHandle *wh;
  258. uint16_t size;
  259. unsigned long long total;
  260. if (GNUNET_OK !=
  261. GNUNET_CONFIGURATION_get_value_filename (cfg,
  262. "STATISTICS",
  263. "DATABASE",
  264. &fn))
  265. {
  266. GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
  267. "STATISTICS",
  268. "DATABASE");
  269. return;
  270. }
  271. (void) GNUNET_DISK_directory_create_for_file (fn);
  272. wh = GNUNET_BIO_write_open (fn);
  273. total = 0;
  274. while (NULL != (pos = start))
  275. {
  276. start = pos->next;
  277. if ((pos->persistent) && (NULL != wh))
  278. {
  279. size = htons (pos->msg->header.size);
  280. if (GNUNET_OK != GNUNET_BIO_write (wh, pos->msg, size))
  281. {
  282. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn);
  283. if (GNUNET_OK != GNUNET_BIO_write_close (wh))
  284. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "close", fn);
  285. wh = NULL;
  286. }
  287. else
  288. total += size;
  289. }
  290. GNUNET_free (pos);
  291. }
  292. if (NULL != wh)
  293. {
  294. if (GNUNET_OK != GNUNET_BIO_write_close (wh))
  295. GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "close", fn);
  296. if (total == 0)
  297. GNUNET_break (0 == UNLINK (fn));
  298. else
  299. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  300. _("Wrote %llu bytes of statistics to `%s'\n"), total, fn);
  301. }
  302. GNUNET_free_non_null (fn);
  303. }
  304. /**
  305. * Transmit the given stats value.
  306. *
  307. * @param client receiver of the value
  308. * @param e value to transmit
  309. */
  310. static void
  311. transmit (struct GNUNET_SERVER_Client *client, const struct StatsEntry *e)
  312. {
  313. struct GNUNET_STATISTICS_ReplyMessage *m;
  314. size_t size;
  315. size =
  316. sizeof (struct GNUNET_STATISTICS_ReplyMessage) + strlen (e->service) + 1 +
  317. strlen (e->name) + 1;
  318. GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
  319. m = GNUNET_malloc (size);
  320. m->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_VALUE);
  321. m->header.size = htons (size);
  322. m->uid = htonl (e->uid);
  323. if (e->persistent)
  324. m->uid |= htonl (GNUNET_STATISTICS_PERSIST_BIT);
  325. m->value = GNUNET_htonll (e->value);
  326. size -= sizeof (struct GNUNET_STATISTICS_ReplyMessage);
  327. GNUNET_assert (size ==
  328. GNUNET_STRINGS_buffer_fill ((char *) &m[1], size, 2,
  329. e->service, e->name));
  330. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  331. "Transmitting value for `%s:%s' (%d): %llu\n", e->service,
  332. e->name, e->persistent, e->value);
  333. GNUNET_SERVER_notification_context_unicast (nc, client, &m->header,
  334. GNUNET_NO);
  335. GNUNET_free (m);
  336. }
  337. /**
  338. * Does this entry match the request?
  339. *
  340. * @param e an entry
  341. * @param service name of service to match
  342. * @param name value to match
  343. * @return 1 if they match, 0 if not
  344. */
  345. static int
  346. matches (const struct StatsEntry *e, const char *service, const char *name)
  347. {
  348. return ((0 == strlen (service)) || (0 == strcmp (service, e->service))) &&
  349. ((0 == strlen (name)) || (0 == strcmp (name, e->name)));
  350. }
  351. /**
  352. * Find a client entry for the given client handle, or create one.
  353. *
  354. * @param client handle to match
  355. * @return corresponding client entry struct
  356. */
  357. static struct ClientEntry *
  358. make_client_entry (struct GNUNET_SERVER_Client *client)
  359. {
  360. struct ClientEntry *ce;
  361. GNUNET_assert (client != NULL);
  362. ce = client_head;
  363. while (ce != NULL)
  364. {
  365. if (ce->client == client)
  366. return ce;
  367. ce = ce->next;
  368. }
  369. if (NULL == nc)
  370. {
  371. GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
  372. return NULL;
  373. }
  374. ce = GNUNET_new (struct ClientEntry);
  375. ce->client = client;
  376. GNUNET_CONTAINER_DLL_insert (client_head, client_tail, ce);
  377. GNUNET_SERVER_notification_context_add (nc, client);
  378. return ce;
  379. }
  380. /**
  381. * Handle GET-message.
  382. *
  383. * @param cls closure
  384. * @param client identification of the client
  385. * @param message the actual message
  386. * @return GNUNET_OK to keep the connection open,
  387. * GNUNET_SYSERR to close it (signal serious error)
  388. */
  389. static void
  390. handle_get (void *cls, struct GNUNET_SERVER_Client *client,
  391. const struct GNUNET_MessageHeader *message)
  392. {
  393. struct GNUNET_MessageHeader end;
  394. char *service;
  395. char *name;
  396. struct StatsEntry *pos;
  397. size_t size;
  398. if ( (NULL != client) &&
  399. (NULL == make_client_entry (client)) )
  400. return; /* new client during shutdown */
  401. size = ntohs (message->size) - sizeof (struct GNUNET_MessageHeader);
  402. if (size !=
  403. GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1], size, 2,
  404. &service, &name))
  405. {
  406. GNUNET_break (0);
  407. GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
  408. return;
  409. }
  410. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  411. "Received request for statistics on `%s:%s'\n",
  412. strlen (service) ? service : "*", strlen (name) ? name : "*");
  413. for (pos = start; NULL != pos; pos = pos->next)
  414. if (matches (pos, service, name))
  415. transmit (client, pos);
  416. end.size = htons (sizeof (struct GNUNET_MessageHeader));
  417. end.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_END);
  418. GNUNET_SERVER_notification_context_unicast (nc, client, &end, GNUNET_NO);
  419. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  420. }
  421. /**
  422. * Notify all clients listening about a change to a value.
  423. *
  424. * @param se value that changed
  425. */
  426. static void
  427. notify_change (struct StatsEntry *se)
  428. {
  429. struct GNUNET_STATISTICS_WatchValueMessage wvm;
  430. struct WatchEntry *pos;
  431. for (pos = se->we_head; NULL != pos; pos = pos->next)
  432. {
  433. if (GNUNET_YES == pos->last_value_set)
  434. {
  435. if (pos->last_value == se->value)
  436. continue;
  437. }
  438. else
  439. {
  440. pos->last_value_set = GNUNET_YES;
  441. }
  442. wvm.header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_WATCH_VALUE);
  443. wvm.header.size =
  444. htons (sizeof (struct GNUNET_STATISTICS_WatchValueMessage));
  445. wvm.flags = htonl (se->persistent ? GNUNET_STATISTICS_PERSIST_BIT : 0);
  446. wvm.wid = htonl (pos->wid);
  447. wvm.reserved = htonl (0);
  448. wvm.value = GNUNET_htonll (se->value);
  449. GNUNET_SERVER_notification_context_unicast (nc, pos->client, &wvm.header,
  450. GNUNET_NO);
  451. pos->last_value = se->value;
  452. }
  453. }
  454. /**
  455. * Handle SET-message.
  456. *
  457. * @param cls closure
  458. * @param client identification of the client
  459. * @param message the actual message
  460. */
  461. static void
  462. handle_set (void *cls, struct GNUNET_SERVER_Client *client,
  463. const struct GNUNET_MessageHeader *message)
  464. {
  465. char *service;
  466. char *name;
  467. uint16_t msize;
  468. uint16_t size;
  469. const struct GNUNET_STATISTICS_SetMessage *msg;
  470. struct StatsEntry *pos;
  471. struct StatsEntry *prev;
  472. uint32_t flags;
  473. uint64_t value;
  474. int64_t delta;
  475. int changed;
  476. int initial_set;
  477. if ( (NULL != client) &&
  478. (NULL == make_client_entry (client)) )
  479. return; /* new client during shutdown */
  480. msize = ntohs (message->size);
  481. if (msize < sizeof (struct GNUNET_STATISTICS_SetMessage))
  482. {
  483. GNUNET_break (0);
  484. GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
  485. return;
  486. }
  487. size = msize - sizeof (struct GNUNET_STATISTICS_SetMessage);
  488. msg = (const struct GNUNET_STATISTICS_SetMessage *) message;
  489. if (size !=
  490. GNUNET_STRINGS_buffer_tokenize ((const char *) &msg[1], size, 2, &service,
  491. &name))
  492. {
  493. GNUNET_break (0);
  494. GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
  495. return;
  496. }
  497. flags = ntohl (msg->flags);
  498. value = GNUNET_ntohll (msg->value);
  499. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  500. "Received request to update statistic on `%s:%s' (%u) to/by %llu\n",
  501. service, name, (unsigned int) flags, (unsigned long long) value);
  502. pos = start;
  503. prev = NULL;
  504. while (pos != NULL)
  505. {
  506. if (matches (pos, service, name))
  507. {
  508. initial_set = 0;
  509. if ((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0)
  510. {
  511. changed = (pos->value != value);
  512. pos->value = value;
  513. }
  514. else
  515. {
  516. delta = (int64_t) value;
  517. if ((delta < 0) && (pos->value < -delta))
  518. {
  519. changed = (pos->value != 0);
  520. pos->value = 0;
  521. }
  522. else
  523. {
  524. changed = (delta != 0);
  525. GNUNET_break ((delta <= 0) || (pos->value + delta > pos->value));
  526. pos->value += delta;
  527. }
  528. }
  529. if (GNUNET_NO == pos->set)
  530. {
  531. pos->set = GNUNET_YES;
  532. initial_set = 1;
  533. }
  534. pos->msg->value = GNUNET_htonll (pos->value);
  535. pos->msg->flags = msg->flags;
  536. pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
  537. if (prev != NULL)
  538. {
  539. /* move to front for faster setting next time! */
  540. prev->next = pos->next;
  541. pos->next = start;
  542. start = pos;
  543. }
  544. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  545. "Statistic `%s:%s' updated to value %llu.\n", service, name,
  546. pos->value);
  547. if ((changed) || (1 == initial_set))
  548. notify_change (pos);
  549. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  550. return;
  551. }
  552. prev = pos;
  553. pos = pos->next;
  554. }
  555. pos = GNUNET_malloc (sizeof (struct StatsEntry) + msize);
  556. pos->next = start;
  557. if (((flags & GNUNET_STATISTICS_SETFLAG_RELATIVE) == 0) ||
  558. (0 < (int64_t) GNUNET_ntohll (msg->value)))
  559. {
  560. pos->value = GNUNET_ntohll (msg->value);
  561. pos->set = GNUNET_YES;
  562. }
  563. else
  564. {
  565. pos->set = GNUNET_NO;
  566. }
  567. pos->uid = uidgen++;
  568. pos->persistent = (0 != (flags & GNUNET_STATISTICS_SETFLAG_PERSISTENT));
  569. pos->msg = (void *) &pos[1];
  570. memcpy (pos->msg, message, ntohs (message->size));
  571. pos->service = (const char *) &pos->msg[1];
  572. pos->name = &pos->service[strlen (pos->service) + 1];
  573. start = pos;
  574. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  575. "New statistic on `%s:%s' with value %llu created.\n", service,
  576. name, pos->value);
  577. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  578. }
  579. /**
  580. * Handle WATCH-message.
  581. *
  582. * @param cls closure
  583. * @param client identification of the client
  584. * @param message the actual message
  585. */
  586. static void
  587. handle_watch (void *cls, struct GNUNET_SERVER_Client *client,
  588. const struct GNUNET_MessageHeader *message)
  589. {
  590. char *service;
  591. char *name;
  592. uint16_t msize;
  593. uint16_t size;
  594. struct StatsEntry *pos;
  595. struct ClientEntry *ce;
  596. struct WatchEntry *we;
  597. size_t slen;
  598. if (NULL == nc)
  599. {
  600. GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
  601. return;
  602. }
  603. GNUNET_SERVER_client_mark_monitor (client);
  604. ce = make_client_entry (client);
  605. msize = ntohs (message->size);
  606. if (msize < sizeof (struct GNUNET_MessageHeader))
  607. {
  608. GNUNET_break (0);
  609. GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
  610. return;
  611. }
  612. size = msize - sizeof (struct GNUNET_MessageHeader);
  613. if (size !=
  614. GNUNET_STRINGS_buffer_tokenize ((const char *) &message[1], size, 2,
  615. &service, &name))
  616. {
  617. GNUNET_break (0);
  618. GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
  619. return;
  620. }
  621. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  622. "Received request to watch statistic on `%s:%s'\n", service,
  623. name);
  624. pos = start;
  625. while (pos != NULL)
  626. {
  627. if (matches (pos, service, name))
  628. break;
  629. pos = pos->next;
  630. }
  631. if (pos == NULL)
  632. {
  633. pos =
  634. GNUNET_malloc (sizeof (struct StatsEntry) +
  635. sizeof (struct GNUNET_STATISTICS_SetMessage) + size);
  636. pos->next = start;
  637. pos->uid = uidgen++;
  638. pos->set = GNUNET_NO;
  639. pos->msg = (void *) &pos[1];
  640. pos->msg->header.size =
  641. htons (sizeof (struct GNUNET_STATISTICS_SetMessage) + size);
  642. pos->msg->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_SET);
  643. pos->service = (const char *) &pos->msg[1];
  644. slen = strlen (service) + 1;
  645. memcpy ((void *) pos->service, service, slen);
  646. pos->name = &pos->service[slen];
  647. memcpy ((void *) pos->name, name, strlen (name) + 1);
  648. start = pos;
  649. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  650. "New statistic on `%s:%s' with value %llu created.\n", service,
  651. name, pos->value);
  652. }
  653. we = GNUNET_new (struct WatchEntry);
  654. we->client = client;
  655. we->last_value_set = GNUNET_NO;
  656. we->wid = ce->max_wid++;
  657. GNUNET_CONTAINER_DLL_insert (pos->we_head, pos->we_tail, we);
  658. if (pos->value != 0)
  659. notify_change (pos);
  660. GNUNET_SERVER_receive_done (client, GNUNET_OK);
  661. }
  662. /**
  663. * Actually perform the shutdown.
  664. */
  665. static void
  666. do_shutdown ()
  667. {
  668. struct WatchEntry *we;
  669. struct StatsEntry *se;
  670. if (NULL == nc)
  671. return;
  672. save ();
  673. GNUNET_SERVER_notification_context_destroy (nc);
  674. nc = NULL;
  675. GNUNET_assert (NULL == client_head);
  676. while (NULL != (se = start))
  677. {
  678. start = se->next;
  679. while (NULL != (we = se->we_head))
  680. {
  681. GNUNET_CONTAINER_DLL_remove (se->we_head, se->we_tail, we);
  682. GNUNET_free (we);
  683. }
  684. GNUNET_free (se);
  685. }
  686. }
  687. /**
  688. * Task run during shutdown.
  689. *
  690. * @param cls unused
  691. * @param tc unused
  692. */
  693. static void
  694. shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  695. {
  696. in_shutdown = GNUNET_YES;
  697. if (NULL != client_head)
  698. return;
  699. do_shutdown ();
  700. }
  701. /**
  702. * A client disconnected. Remove all of its data structure entries.
  703. *
  704. * @param cls closure, NULL
  705. * @param client identification of the client
  706. */
  707. static void
  708. handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
  709. {
  710. struct ClientEntry *ce;
  711. struct WatchEntry *we;
  712. struct WatchEntry *wen;
  713. struct StatsEntry *se;
  714. ce = client_head;
  715. while (NULL != ce)
  716. {
  717. if (ce->client == client)
  718. {
  719. GNUNET_CONTAINER_DLL_remove (client_head, client_tail, ce);
  720. GNUNET_free (ce);
  721. break;
  722. }
  723. ce = ce->next;
  724. }
  725. se = start;
  726. while (NULL != se)
  727. {
  728. wen = se->we_head;
  729. while (NULL != (we = wen))
  730. {
  731. wen = we->next;
  732. if (we->client != client)
  733. continue;
  734. GNUNET_CONTAINER_DLL_remove (se->we_head, se->we_tail, we);
  735. GNUNET_free (we);
  736. }
  737. se = se->next;
  738. }
  739. if ( (NULL == client_head) &&
  740. (GNUNET_YES == in_shutdown) )
  741. do_shutdown ();
  742. }
  743. /**
  744. * Process statistics requests.
  745. *
  746. * @param cls closure
  747. * @param server the initialized server
  748. * @param c configuration to use
  749. */
  750. static void
  751. run (void *cls, struct GNUNET_SERVER_Handle *server,
  752. const struct GNUNET_CONFIGURATION_Handle *c)
  753. {
  754. static const struct GNUNET_SERVER_MessageHandler handlers[] = {
  755. {&handle_set, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_SET, 0},
  756. {&handle_get, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_GET, 0},
  757. {&handle_watch, NULL, GNUNET_MESSAGE_TYPE_STATISTICS_WATCH, 0},
  758. {NULL, NULL, 0, 0}
  759. };
  760. cfg = c;
  761. srv = server;
  762. GNUNET_SERVER_add_handlers (server, handlers);
  763. nc = GNUNET_SERVER_notification_context_create (server, 16);
  764. GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL);
  765. load (server);
  766. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
  767. NULL);
  768. }
  769. /**
  770. * The main function for the statistics service.
  771. *
  772. * @param argc number of arguments from the command line
  773. * @param argv command line arguments
  774. * @return 0 ok, 1 on error
  775. */
  776. int
  777. main (int argc, char *const *argv)
  778. {
  779. return (GNUNET_OK ==
  780. GNUNET_SERVICE_run (argc, argv, "statistics",
  781. GNUNET_SERVICE_OPTION_SOFT_SHUTDOWN, &run, NULL)) ? 0 : 1;
  782. }
  783. #ifdef LINUX
  784. #include <malloc.h>
  785. /**
  786. * MINIMIZE heap size (way below 128k) since this process doesn't need much.
  787. */
  788. void __attribute__ ((constructor)) GNUNET_ARM_memory_init ()
  789. {
  790. mallopt (M_TRIM_THRESHOLD, 4 * 1024);
  791. mallopt (M_TOP_PAD, 1 * 1024);
  792. malloc_trim (0);
  793. }
  794. #endif
  795. /* end of gnunet-service-statistics.c */