update.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. /******************************************************************************
  2. * This file is part of TinTin++ *
  3. * *
  4. * Copyright 2004-2019 Igor van den Hoven *
  5. * *
  6. * TinTin++ is free software; you can redistribute it and/or modify *
  7. * it under the terms of the GNU General Public License as published by *
  8. * the Free Software Foundation; either version 3 of the License, or *
  9. * (at your option) any later version. *
  10. * *
  11. * This program is distributed in the hope that it will be useful, *
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  14. * GNU General Public License for more details. *
  15. * *
  16. * *
  17. * You should have received a copy of the GNU General Public License *
  18. * along with TinTin++. If not, see https://www.gnu.org/licenses. *
  19. ******************************************************************************/
  20. /******************************************************************************
  21. * (T)he K(I)cki(N) (T)ickin D(I)kumud Clie(N)t *
  22. * *
  23. * coded by Igor van den Hoven 2006 *
  24. ******************************************************************************/
  25. #include "tintin.h"
  26. #include <sys/types.h>
  27. #include <sys/time.h>
  28. #include <termios.h>
  29. void mainloop(void)
  30. {
  31. static struct timeval curr_time, wait_time, last_time;
  32. int usec_loop, usec_wait;
  33. short int pulse_poll_input = 0 + PULSE_POLL_INPUT;
  34. short int pulse_poll_sessions = 0 + PULSE_POLL_SESSIONS;
  35. short int pulse_poll_chat = 0 + PULSE_POLL_CHAT;
  36. short int pulse_poll_port = 0 + PULSE_POLL_PORT;
  37. short int pulse_update_ticks = 0 + PULSE_UPDATE_TICKS;
  38. short int pulse_update_delays = 0 + PULSE_UPDATE_DELAYS;
  39. short int pulse_update_packets = 0 + PULSE_UPDATE_PACKETS;
  40. short int pulse_update_chat = 0 + PULSE_UPDATE_CHAT;
  41. short int pulse_update_terminal = 0 + PULSE_UPDATE_TERMINAL;
  42. short int pulse_update_memory = 0 + PULSE_UPDATE_MEMORY;
  43. short int pulse_update_time = 1 + PULSE_UPDATE_TIME;
  44. wait_time.tv_sec = 0;
  45. push_call("mainloop()");
  46. while (TRUE)
  47. {
  48. gettimeofday(&last_time, NULL);
  49. if (--pulse_poll_input == 0)
  50. {
  51. open_timer(TIMER_POLL_INPUT);
  52. pulse_poll_input = PULSE_POLL_INPUT;
  53. poll_input();
  54. close_timer(TIMER_POLL_INPUT);
  55. }
  56. if (--pulse_poll_sessions == 0)
  57. {
  58. pulse_poll_sessions = PULSE_POLL_SESSIONS;
  59. poll_sessions();
  60. }
  61. if (--pulse_poll_chat == 0)
  62. {
  63. pulse_poll_chat = PULSE_POLL_CHAT;
  64. poll_chat();
  65. }
  66. if (--pulse_poll_port == 0)
  67. {
  68. pulse_poll_port = PULSE_POLL_PORT;
  69. poll_port();
  70. }
  71. if (--pulse_update_ticks == 0)
  72. {
  73. pulse_update_ticks = PULSE_UPDATE_TICKS;
  74. tick_update();
  75. }
  76. if (--pulse_update_delays == 0)
  77. {
  78. pulse_update_delays = PULSE_UPDATE_DELAYS;
  79. delay_update();
  80. }
  81. if (--pulse_update_packets == 0)
  82. {
  83. pulse_update_packets = PULSE_UPDATE_PACKETS;
  84. packet_update();
  85. }
  86. if (--pulse_update_chat == 0)
  87. {
  88. pulse_update_chat = PULSE_UPDATE_CHAT;
  89. chat_update();
  90. }
  91. if (--pulse_update_terminal == 0)
  92. {
  93. pulse_update_terminal = PULSE_UPDATE_TERMINAL;
  94. terminal_update();
  95. }
  96. if (--pulse_update_memory == 0)
  97. {
  98. pulse_update_memory = PULSE_UPDATE_MEMORY;
  99. memory_update();
  100. }
  101. if (--pulse_update_time == 0)
  102. {
  103. pulse_update_time = PULSE_UPDATE_TIME;
  104. time_update();
  105. }
  106. gettimeofday(&curr_time, NULL);
  107. if (curr_time.tv_sec == last_time.tv_sec)
  108. {
  109. usec_loop = curr_time.tv_usec - last_time.tv_usec;
  110. }
  111. else
  112. {
  113. usec_loop = 1000000 - last_time.tv_usec + curr_time.tv_usec;
  114. }
  115. usec_wait = 1000000 / PULSE_PER_SECOND - usec_loop;
  116. wait_time.tv_usec = usec_wait;
  117. gtd->total_io_exec += usec_loop;
  118. gtd->total_io_delay += usec_wait;
  119. if (usec_wait > 0)
  120. {
  121. select(0, NULL, NULL, NULL, &wait_time);
  122. }
  123. }
  124. pop_call();
  125. return;
  126. }
  127. void poll_input(void)
  128. {
  129. fd_set readfds;
  130. static struct timeval to;
  131. while (TRUE)
  132. {
  133. FD_ZERO(&readfds);
  134. FD_SET(0, &readfds);
  135. if (select(FD_SETSIZE, &readfds, NULL, NULL, &to) <= 0)
  136. {
  137. return;
  138. }
  139. if (FD_ISSET(0, &readfds))
  140. {
  141. process_input();
  142. }
  143. else
  144. {
  145. return;
  146. }
  147. }
  148. }
  149. void poll_sessions(void)
  150. {
  151. fd_set readfds, excfds;
  152. static struct timeval to;
  153. struct session *ses;
  154. int rv;
  155. push_call("poll_sessions(void)");
  156. open_timer(TIMER_POLL_SESSIONS);
  157. if (gts->next)
  158. {
  159. FD_ZERO(&readfds);
  160. FD_ZERO(&excfds);
  161. for (ses = gts->next ; ses ; ses = gtd->update)
  162. {
  163. gtd->update = ses->next;
  164. if (HAS_BIT(ses->flags, SES_FLAG_CONNECTED))
  165. {
  166. while (TRUE)
  167. {
  168. FD_SET(ses->socket, &readfds);
  169. FD_SET(ses->socket, &excfds);
  170. rv = select(FD_SETSIZE, &readfds, NULL, &excfds, &to);
  171. if (rv < 0)
  172. {
  173. break;
  174. // ses->
  175. syserr_printf(ses, "poll_sessions: select = %d:", rv);
  176. cleanup_session(ses);
  177. gtd->mud_output_len = 0;
  178. break;;
  179. }
  180. if (rv == 0)
  181. {
  182. break;
  183. }
  184. if (FD_ISSET(ses->socket, &readfds))
  185. {
  186. if (read_buffer_mud(ses) == FALSE)
  187. {
  188. readmud(ses);
  189. cleanup_session(ses);
  190. gtd->mud_output_len = 0;
  191. break;
  192. }
  193. }
  194. if (FD_ISSET(ses->socket, &excfds))
  195. {
  196. FD_CLR(ses->socket, &readfds);
  197. cleanup_session(ses);
  198. gtd->mud_output_len = 0;
  199. break;
  200. }
  201. }
  202. if (gtd->mud_output_len)
  203. {
  204. readmud(ses);
  205. }
  206. }
  207. }
  208. }
  209. close_timer(TIMER_POLL_SESSIONS);
  210. pop_call();
  211. return;
  212. }
  213. void poll_chat(void)
  214. {
  215. fd_set readfds, writefds, excfds;
  216. static struct timeval to;
  217. struct chat_data *buddy;
  218. int rv;
  219. open_timer(TIMER_POLL_CHAT);
  220. if (gtd->chat)
  221. {
  222. FD_ZERO(&readfds);
  223. FD_ZERO(&writefds);
  224. FD_ZERO(&excfds);
  225. FD_SET(gtd->chat->fd, &readfds);
  226. for (buddy = gtd->chat->next ; buddy ; buddy = buddy->next)
  227. {
  228. FD_SET(buddy->fd, &readfds);
  229. FD_SET(buddy->fd, &writefds);
  230. FD_SET(buddy->fd, &excfds);
  231. }
  232. rv = select(FD_SETSIZE, &readfds, &writefds, &excfds, &to);
  233. if (rv <= 0)
  234. {
  235. if (rv == 0 || errno == EINTR)
  236. {
  237. goto poll_chat_end;
  238. }
  239. syserr_fatal(-1, "poll_chat: select");
  240. }
  241. process_chat_connections(&readfds, &writefds, &excfds);
  242. }
  243. poll_chat_end:
  244. close_timer(TIMER_POLL_CHAT);
  245. }
  246. void poll_port(void)
  247. {
  248. struct session *ses;
  249. fd_set readfds, writefds, excfds;
  250. static struct timeval to;
  251. struct port_data *buddy;
  252. int rv;
  253. open_timer(TIMER_POLL_PORT);
  254. for (ses = gts->next ; ses ; ses = gtd->update)
  255. {
  256. gtd->update = ses->next;
  257. if (ses->port)
  258. {
  259. FD_ZERO(&readfds);
  260. FD_ZERO(&writefds);
  261. FD_ZERO(&excfds);
  262. FD_SET(ses->port->fd, &readfds);
  263. for (buddy = ses->port->next ; buddy ; buddy = buddy->next)
  264. {
  265. FD_SET(buddy->fd, &readfds);
  266. FD_SET(buddy->fd, &writefds);
  267. FD_SET(buddy->fd, &excfds);
  268. }
  269. rv = select(FD_SETSIZE, &readfds, &writefds, &excfds, &to);
  270. if (rv <= 0)
  271. {
  272. if (rv == 0 || errno == EINTR)
  273. {
  274. continue;
  275. }
  276. syserr_fatal(-1, "poll_port: select");
  277. }
  278. process_port_connections(ses, &readfds, &writefds, &excfds);
  279. }
  280. }
  281. close_timer(TIMER_POLL_PORT);
  282. }
  283. void tick_update(void)
  284. {
  285. struct session *ses;
  286. struct listnode *node;
  287. struct listroot *root;
  288. open_timer(TIMER_UPDATE_TICKS);
  289. utime();
  290. for (ses = gts->next ; ses ; ses = gtd->update)
  291. {
  292. gtd->update = ses->next;
  293. root = ses->list[LIST_TICKER];
  294. for (root->update = 0 ; root->update < root->used ; root->update++)
  295. {
  296. node = root->list[root->update];
  297. if (node->data == 0)
  298. {
  299. node->data = gtd->utime + (long long) (get_number(ses, node->arg3) * 1000000LL);
  300. show_info(ses, LIST_DELAY, "#INFO TICK {%s} INITIALIZED WITH TIMESTAMP {%lld}", node->arg1, node->data);
  301. }
  302. if (node->data <= gtd->utime)
  303. {
  304. node->data += (long long) (get_number(ses, node->arg3) * 1000000LL);
  305. show_info(ses, LIST_DELAY, "#INFO TICK {%s} INITIALIZED WITH TIMESTAMP {%lld}", node->arg1, node->data);
  306. if (!HAS_BIT(root->flags, LIST_FLAG_IGNORE))
  307. {
  308. show_debug(ses, LIST_TICKER, "#DEBUG TICKER {%s}", node->arg2);
  309. script_driver(ses, LIST_TICKER, node->arg2);
  310. }
  311. }
  312. }
  313. }
  314. close_timer(TIMER_UPDATE_TICKS);
  315. }
  316. void delay_update(void)
  317. {
  318. struct session *ses;
  319. struct listnode *node;
  320. struct listroot *root;
  321. char buf[BUFFER_SIZE];
  322. open_timer(TIMER_UPDATE_DELAYS);
  323. for (ses = gts ; ses ; ses = gtd->update)
  324. {
  325. gtd->update = ses->next;
  326. root = ses->list[LIST_DELAY];
  327. for (root->update = 0 ; root->update < root->used ; root->update++)
  328. {
  329. node = root->list[root->update];
  330. if (node->data == 0)
  331. {
  332. node->data = gtd->utime + (long long) (get_number(ses, node->arg3) * 1000000LL);
  333. show_info(ses, LIST_DELAY, "#INFO DELAY {%s} INITIALIZED WITH TIMESTAMP {%lld}", node->arg1, node->data);
  334. }
  335. if (node->data <= gtd->utime)
  336. {
  337. strcpy(buf, node->arg2);
  338. show_debug(ses, LIST_DELAY, "#DEBUG DELAY {%s}", buf);
  339. delete_node_list(ses, LIST_DELAY, node);
  340. script_driver(ses, LIST_DELAY, buf);
  341. }
  342. }
  343. }
  344. close_timer(TIMER_UPDATE_DELAYS);
  345. }
  346. void packet_update(void)
  347. {
  348. char result[STRING_SIZE];
  349. struct session *ses;
  350. open_timer(TIMER_UPDATE_PACKETS);
  351. for (ses = gts->next ; ses ; ses = gtd->update)
  352. {
  353. gtd->update = ses->next;
  354. if (ses->check_output && gtd->utime > ses->check_output)
  355. {
  356. if (HAS_BIT(ses->flags, SES_FLAG_SPLIT))
  357. {
  358. save_pos(ses);
  359. goto_rowcol(ses, ses->bot_row, 1);
  360. }
  361. SET_BIT(ses->flags, SES_FLAG_READMUD);
  362. strcpy(result, ses->more_output);
  363. ses->more_output[0] = 0;
  364. process_mud_output(ses, result, TRUE);
  365. DEL_BIT(ses->flags, SES_FLAG_READMUD);
  366. if (HAS_BIT(ses->flags, SES_FLAG_SPLIT))
  367. {
  368. restore_pos(ses);
  369. }
  370. }
  371. }
  372. close_timer(TIMER_UPDATE_PACKETS);
  373. }
  374. void chat_update(void)
  375. {
  376. struct chat_data *buddy, *buddy_next;
  377. open_timer(TIMER_UPDATE_CHAT);
  378. if (gtd->chat)
  379. {
  380. for (buddy = gtd->chat->next ; buddy ; buddy = buddy_next)
  381. {
  382. buddy_next = buddy->next;
  383. if (buddy->timeout && buddy->timeout < gtd->time)
  384. {
  385. chat_socket_printf(buddy, "<CHAT> Connection timed out.");
  386. close_chat(buddy, TRUE);
  387. }
  388. }
  389. if (gtd->chat->paste_time && gtd->chat->paste_time < gtd->utime)
  390. {
  391. chat_paste(NULL, NULL);
  392. }
  393. }
  394. close_timer(TIMER_UPDATE_CHAT);
  395. }
  396. void terminal_update(void)
  397. {
  398. struct session *ses;
  399. open_timer(TIMER_UPDATE_TERMINAL);
  400. for (ses = gts ; ses ; ses = ses->next)
  401. {
  402. if (HAS_BIT(ses->flags, SES_FLAG_UPDATEVTMAP))
  403. {
  404. DEL_BIT(ses->flags, SES_FLAG_UPDATEVTMAP);
  405. show_vtmap(ses);
  406. check_all_events(ses, SUB_ARG|SUB_SEC, 0, 0, "MAP UPDATED VTMAP");
  407. }
  408. }
  409. fflush(stdout);
  410. close_timer(TIMER_UPDATE_TERMINAL);
  411. }
  412. void memory_update(void)
  413. {
  414. open_timer(TIMER_UPDATE_MEMORY);
  415. while (gtd->dispose_next)
  416. {
  417. dispose_session(gtd->dispose_next);
  418. }
  419. close_timer(TIMER_UPDATE_MEMORY);
  420. }
  421. void time_update(void)
  422. {
  423. static char str_sec[9], str_min[9], str_hour[9], str_wday[9], str_mday[9], str_mon[9], str_year[9];
  424. static struct tm old_calendar;
  425. gtd->time = time(NULL);
  426. gtd->calendar = *localtime(&gtd->time);
  427. open_timer(TIMER_UPDATE_TIME);
  428. // Initialize on the first call.
  429. if (old_calendar.tm_year == 0)
  430. {
  431. old_calendar.tm_sec = gtd->calendar.tm_sec;
  432. old_calendar.tm_min = gtd->calendar.tm_min;
  433. old_calendar.tm_hour = gtd->calendar.tm_hour;
  434. old_calendar.tm_wday = gtd->calendar.tm_wday;
  435. old_calendar.tm_mday = gtd->calendar.tm_mday;
  436. old_calendar.tm_mon = gtd->calendar.tm_mon;
  437. old_calendar.tm_year = gtd->calendar.tm_year;
  438. strftime(str_sec, 9, "%S", &gtd->calendar);
  439. strftime(str_min, 9, "%M", &gtd->calendar);
  440. strftime(str_hour, 9, "%H", &gtd->calendar);
  441. strftime(str_wday, 9, "%w", &gtd->calendar);
  442. strftime(str_mday, 9, "%d", &gtd->calendar);
  443. strftime(str_mon, 9, "%m", &gtd->calendar);
  444. strftime(str_year, 9, "%Y", &gtd->calendar);
  445. return;
  446. }
  447. if (gtd->calendar.tm_sec == old_calendar.tm_sec)
  448. {
  449. goto time_event_end;
  450. }
  451. strftime(str_min, 9, "%S", &gtd->calendar);
  452. old_calendar.tm_sec = gtd->calendar.tm_sec;
  453. if (gtd->calendar.tm_min == old_calendar.tm_min)
  454. {
  455. goto time_event_sec;
  456. }
  457. strftime(str_min, 9, "%M", &gtd->calendar);
  458. old_calendar.tm_min = gtd->calendar.tm_min;
  459. if (gtd->calendar.tm_hour == old_calendar.tm_hour)
  460. {
  461. goto time_event_min;
  462. }
  463. strftime(str_hour, 9, "%H", &gtd->calendar);
  464. old_calendar.tm_hour = gtd->calendar.tm_hour;
  465. if (gtd->calendar.tm_mday == old_calendar.tm_mday)
  466. {
  467. goto time_event_hour;
  468. }
  469. strftime(str_wday, 9, "%w", &gtd->calendar);
  470. old_calendar.tm_wday = gtd->calendar.tm_wday;
  471. strftime(str_mday, 9, "%d", &gtd->calendar);
  472. old_calendar.tm_mday = gtd->calendar.tm_mday;
  473. if (gtd->calendar.tm_mon == old_calendar.tm_mon)
  474. {
  475. goto time_event_mday;
  476. }
  477. strftime(str_mon, 9, "%m", &gtd->calendar);
  478. old_calendar.tm_mon = gtd->calendar.tm_mon;
  479. if (gtd->calendar.tm_year == old_calendar.tm_year)
  480. {
  481. goto time_event_mon;
  482. }
  483. strftime(str_year, 9, "%Y", &gtd->calendar);
  484. old_calendar.tm_year = gtd->calendar.tm_year;
  485. check_all_events(NULL, SUB_ARG, 0, 7, "YEAR", str_year, str_mon, str_wday, str_mday, str_hour, str_min, str_sec);
  486. check_all_events(NULL, SUB_ARG, 1, 7, "YEAR %s", str_year, str_year, str_mon, str_wday, str_mday, str_hour, str_min, str_sec);
  487. time_event_mon:
  488. check_all_events(NULL, SUB_ARG, 0, 7, "MONTH", str_year, str_mon, str_wday, str_mday, str_hour, str_min, str_sec);
  489. check_all_events(NULL, SUB_ARG, 1, 7, "MONTH %s", str_mon, str_year, str_mon, str_wday, str_mday, str_hour, str_min, str_sec);
  490. time_event_mday:
  491. check_all_events(NULL, SUB_ARG, 0, 7, "WEEK", str_year, str_mon, str_wday, str_mday, str_hour, str_min, str_sec);
  492. check_all_events(NULL, SUB_ARG, 1, 7, "WEEK %s", str_wday, str_year, str_mon, str_wday, str_mday, str_hour, str_min, str_sec);
  493. check_all_events(NULL, SUB_ARG, 2, 7, "DATE %s-%s", str_mon, str_mday, str_year, str_mon, str_wday, str_mday, str_hour, str_min, str_sec);
  494. check_all_events(NULL, SUB_ARG, 0, 7, "DAY", str_year, str_mon, str_wday, str_mday, str_hour, str_min, str_sec);
  495. check_all_events(NULL, SUB_ARG, 1, 7, "DAY %s", str_mday, str_year, str_mon, str_wday, str_mday, str_hour, str_min, str_sec);
  496. time_event_hour:
  497. check_all_events(NULL, SUB_ARG, 0, 7, "HOUR", str_year, str_mon, str_wday, str_mday, str_hour, str_min, str_sec);
  498. check_all_events(NULL, SUB_ARG, 1, 7, "HOUR %s", str_hour, str_year, str_mon, str_wday, str_mday, str_hour, str_min, str_sec);
  499. time_event_min:
  500. check_all_events(NULL, SUB_ARG, 4, 7, "DATE %s-%s %s:%s", str_mon, str_mday, str_hour, str_min, str_year, str_mon, str_wday, str_mday, str_hour, str_min, str_sec);
  501. check_all_events(NULL, SUB_ARG, 2, 7, "TIME %s:%s", str_hour, str_min, str_year, str_mon, str_wday, str_mday, str_hour, str_min, str_sec);
  502. check_all_events(NULL, SUB_ARG, 0, 7, "MINUTE", str_year, str_mon, str_wday, str_mday, str_hour, str_min, str_sec);
  503. check_all_events(NULL, SUB_ARG, 1, 7, "MINUTE %s", str_min, str_year, str_mon, str_wday, str_mday, str_hour, str_min, str_sec);
  504. time_event_sec:
  505. old_calendar.tm_sec = gtd->calendar.tm_sec;
  506. check_all_events(NULL, SUB_ARG, 3, 7, "TIME %s:%s:%s", str_hour, str_min, str_sec, str_year, str_mon, str_wday, str_mday, str_hour, str_min, str_sec);
  507. check_all_events(NULL, SUB_ARG, 0, 7, "SECOND", str_year, str_mon, str_wday, str_mday, str_hour, str_min, str_sec);
  508. check_all_events(NULL, SUB_ARG, 1, 7, "SECOND %s", str_sec, str_year, str_mon, str_wday, str_mday, str_hour, str_min, str_sec);
  509. time_event_end:
  510. close_timer(TIMER_UPDATE_TIME);
  511. }
  512. void show_cpu(struct session *ses)
  513. {
  514. long long total_cpu;
  515. int timer;
  516. tintin_printf2(ses, "Section Time (usec) Freq (msec) %%Prog %%CPU");
  517. tintin_printf2(ses, "");
  518. for (total_cpu = timer = 0 ; timer < TIMER_CPU ; timer++)
  519. {
  520. total_cpu += display_timer(ses, timer);
  521. }
  522. tintin_printf2(ses, "");
  523. tintin_printf2(ses, "Unknown CPU Usage: %7.3f percent", (gtd->total_io_exec - total_cpu) * 100.0 / (gtd->total_io_delay + gtd->total_io_exec));
  524. tintin_printf2(ses, "Average CPU Usage: %7.3f percent", (gtd->total_io_exec) * 100.0 / (gtd->total_io_delay + gtd->total_io_exec));
  525. }
  526. long long display_timer(struct session *ses, int timer)
  527. {
  528. long long total_usage, indicated_usage;
  529. total_usage = gtd->total_io_exec + gtd->total_io_delay;
  530. if (total_usage == 0)
  531. {
  532. return 0;
  533. }
  534. if (gtd->timer[timer][1] == 0 || gtd->timer[timer][4] == 0)
  535. {
  536. return 0;
  537. }
  538. indicated_usage = gtd->timer[timer][0] / gtd->timer[timer][1] * gtd->timer[timer][4];
  539. tintin_printf2(ses, "%-30s%8lld %8lld %8.2f %8.3f",
  540. timer_table[timer].name,
  541. gtd->timer[timer][0] / gtd->timer[timer][1],
  542. gtd->timer[timer][3] / gtd->timer[timer][4] / 1000,
  543. 100.0 * (double) indicated_usage / (double) gtd->total_io_exec,
  544. 100.0 * (double) indicated_usage / (double) total_usage);
  545. return indicated_usage;
  546. }
  547. void open_timer(int timer)
  548. {
  549. struct timeval last_time;
  550. long long current_time;
  551. gettimeofday(&last_time, NULL);
  552. current_time = (long long) last_time.tv_usec + 1000000LL * (long long) last_time.tv_sec;
  553. if (gtd->timer[timer][2] == 0)
  554. {
  555. gtd->timer[timer][2] = current_time ;
  556. }
  557. else
  558. {
  559. gtd->timer[timer][3] += current_time - gtd->timer[timer][2];
  560. gtd->timer[timer][2] = current_time;
  561. gtd->timer[timer][4] ++;
  562. }
  563. }
  564. void close_timer(int timer)
  565. {
  566. struct timeval last_time;
  567. long long current_time;
  568. gettimeofday(&last_time, NULL);
  569. current_time = (long long) last_time.tv_usec + 1000000LL * (long long) last_time.tv_sec;
  570. gtd->timer[timer][0] += (current_time - gtd->timer[timer][2]);
  571. gtd->timer[timer][1] ++;
  572. }