shim_ipc_child.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * shim_ipc_helper.c
  17. *
  18. * This file contains functions and callbacks to handle IPC between parent
  19. * processes and their children.
  20. */
  21. #include <shim_internal.h>
  22. #include <shim_thread.h>
  23. #include <shim_handle.h>
  24. #include <shim_ipc.h>
  25. #include <shim_utils.h>
  26. #include <shim_profile.h>
  27. #include <pal.h>
  28. #include <pal_error.h>
  29. #include <errno.h>
  30. static int ipc_thread_exit (IDTYPE vmid, IDTYPE ppid, IDTYPE tid,
  31. unsigned int exitcode, unsigned int term_signal, unsigned long exit_time)
  32. {
  33. assert(vmid != cur_process.vmid);
  34. #ifdef PROFILE
  35. if (!exit_time)
  36. exit_time = GET_PROFILE_INTERVAL();
  37. #endif
  38. struct shim_thread * thread = __lookup_thread(tid);
  39. if (thread) {
  40. int ret = 0;
  41. //assert(thread->vmid == vmid && !thread->in_vm);
  42. thread->exit_code = -exitcode;
  43. thread->term_signal = term_signal;
  44. #ifdef PROFILE
  45. thread->exit_time = exit_time;
  46. #endif
  47. ret = thread_exit(thread, false);
  48. put_thread(thread);
  49. return ret;
  50. }
  51. struct shim_simple_thread * sthread = __lookup_simple_thread(tid);
  52. if (!sthread) {
  53. sthread = get_new_simple_thread();
  54. sthread->vmid = vmid;
  55. sthread->tid = tid;
  56. add_simple_thread(sthread);
  57. }
  58. sthread->is_alive = 0;
  59. sthread->exit_code = -exitcode;
  60. sthread->term_signal = term_signal;
  61. #ifdef PROFILE
  62. sthread->exit_time = exit_time;
  63. #endif
  64. DkEventSet(sthread->exit_event);
  65. put_simple_thread(sthread);
  66. return 0;
  67. }
  68. void ipc_parent_exit (struct shim_ipc_port * port, IDTYPE vmid,
  69. unsigned int exitcode, unsigned int term_signal)
  70. {
  71. debug("ipc port %p of process %u closed suggests parent exiting\n",
  72. port, vmid);
  73. struct shim_ipc_info * parent = NULL;
  74. lock(cur_process.lock);
  75. if (parent && vmid == cur_process.parent->vmid) {
  76. parent = cur_process.parent;
  77. cur_process.parent = NULL;
  78. }
  79. unlock(cur_process.lock);
  80. if (parent)
  81. put_ipc_info(parent);
  82. }
  83. struct thread_info {
  84. IDTYPE vmid;
  85. unsigned int exitcode;
  86. unsigned int term_signal;
  87. };
  88. static int child_sthread_exit (struct shim_simple_thread * thread, void * arg,
  89. bool * unlocked)
  90. {
  91. struct thread_info * info = (struct thread_info *) arg;
  92. if (thread->vmid == info->vmid) {
  93. if (thread->is_alive) {
  94. thread->exit_code = -info->exitcode;
  95. thread->term_signal = info->term_signal;
  96. thread->is_alive = false;
  97. DkEventSet(thread->exit_event);
  98. }
  99. return 1;
  100. }
  101. return 0;
  102. }
  103. static int child_thread_exit (struct shim_thread * thread, void * arg,
  104. bool * unlocked)
  105. {
  106. struct thread_info * info = (struct thread_info *) arg;
  107. if (thread->vmid == info->vmid) {
  108. if (thread->is_alive) {
  109. thread->exit_code = -info->exitcode;
  110. thread->term_signal = info->term_signal;
  111. thread_exit(thread, false);
  112. }
  113. return 1;
  114. }
  115. return 0;
  116. }
  117. int remove_child_thread (IDTYPE vmid, unsigned int exitcode, unsigned int term_signal)
  118. {
  119. struct thread_info info = { .vmid = vmid, .exitcode = exitcode, .term_signal = term_signal };
  120. int nkilled = 0, ret;
  121. assert(vmid != cur_process.vmid);
  122. if ((ret = walk_thread_list(&child_thread_exit, &info, false)) > 0)
  123. nkilled += ret;
  124. if ((ret = walk_simple_thread_list(&child_sthread_exit, &info, false)) > 0)
  125. nkilled += ret;
  126. if (!nkilled)
  127. debug("child port closed, no thread exited\n");
  128. return 0;
  129. }
  130. void ipc_child_exit (struct shim_ipc_port * port, IDTYPE vmid,
  131. unsigned int exitcode, unsigned int term_signal)
  132. {
  133. debug("ipc port %p of process %u closed suggests child exiting\n",
  134. port, vmid);
  135. remove_child_thread(vmid, 0, term_signal);
  136. }
  137. static struct shim_ipc_port * get_parent_port (IDTYPE * dest)
  138. {
  139. struct shim_ipc_port * port = NULL;
  140. lock(cur_process.lock);
  141. if (cur_process.parent && (port = cur_process.parent->port)) {
  142. get_ipc_port(port);
  143. *dest = cur_process.parent->vmid;
  144. }
  145. unlock(cur_process.lock);
  146. return port;
  147. }
  148. DEFINE_PROFILE_INTERVAL(ipc_cld_exit_turnaround, ipc);
  149. DEFINE_PROFILE_INTERVAL(ipc_cld_exit_send, ipc);
  150. DEFINE_PROFILE_INTERVAL(ipc_cld_exit_callback, ipc);
  151. int ipc_cld_exit_send (IDTYPE ppid, IDTYPE tid, unsigned int exitcode, unsigned int term_signal)
  152. {
  153. unsigned long send_time = GET_PROFILE_INTERVAL();
  154. BEGIN_PROFILE_INTERVAL_SET(send_time);
  155. int ret = 0;
  156. struct shim_ipc_msg * msg =
  157. create_ipc_msg_on_stack(IPC_CLD_EXIT,
  158. sizeof(struct shim_ipc_cld_exit), 0);
  159. struct shim_ipc_cld_exit * msgin =
  160. (struct shim_ipc_cld_exit *) &msg->msg;
  161. msgin->ppid = ppid;
  162. msgin->tid = tid;
  163. msgin->exitcode = exitcode;
  164. msgin->term_signal = term_signal;
  165. #ifdef PROFILE
  166. msgin->time = send_time;
  167. #endif
  168. debug("ipc broadcast: IPC_CLD_EXIT(%u, %u, %d)\n", ppid, tid, exitcode);
  169. ret = broadcast_ipc(msg, NULL, 0, IPC_PORT_DIRPRT|IPC_PORT_DIRCLD);
  170. SAVE_PROFILE_INTERVAL(ipc_cld_exit_send);
  171. return ret;
  172. }
  173. int ipc_cld_exit_callback (IPC_CALLBACK_ARGS)
  174. {
  175. struct shim_ipc_cld_exit * msgin =
  176. (struct shim_ipc_cld_exit *) &msg->msg;
  177. #ifdef PROFILE
  178. unsigned long time = msgin->time;
  179. #else
  180. unsigned long time = 0;
  181. #endif
  182. BEGIN_PROFILE_INTERVAL_SET(time);
  183. SAVE_PROFILE_INTERVAL(ipc_cld_exit_turnaround);
  184. debug("ipc callback from %u: IPC_CLD_EXIT(%u, %u, %d)\n",
  185. msg->src, msgin->ppid, msgin->tid, msgin->exitcode);
  186. int ret = ipc_thread_exit(msg->src, msgin->ppid, msgin->tid,
  187. msgin->exitcode, msgin->term_signal,
  188. time);
  189. SAVE_PROFILE_INTERVAL(ipc_cld_exit_callback);
  190. return ret;
  191. }
  192. DEFINE_PROFILE_INTERVAL(ipc_cld_join_send, ipc);
  193. DEFINE_PROFILE_INTERVAL(ipc_cld_join_callback, ipc);
  194. int ipc_cld_join_send (IDTYPE dest)
  195. {
  196. BEGIN_PROFILE_INTERVAL();
  197. struct shim_ipc_port * port = dest ?
  198. lookup_ipc_port(dest, IPC_PORT_DIRPRT) :
  199. get_parent_port(&dest);
  200. if (!port)
  201. return -ESRCH;
  202. struct shim_ipc_msg * msg =
  203. create_ipc_msg_on_stack(IPC_CLD_JOIN, 0, dest);
  204. debug("ipc send to %u: IPC_CLD_JOIN\n", dest);
  205. int ret = send_ipc_message(msg, port);
  206. add_ipc_port(port, dest, IPC_PORT_DIRPRT, NULL);
  207. put_ipc_port(port);
  208. SAVE_PROFILE_INTERVAL(ipc_cld_join_send);
  209. return ret;
  210. }
  211. int ipc_cld_join_callback (IPC_CALLBACK_ARGS)
  212. {
  213. BEGIN_PROFILE_INTERVAL();
  214. debug("ipc callback from %u: IPC_CLD_JOIN\n", msg->src);
  215. add_ipc_port(port, msg->src, IPC_PORT_DIRCLD, NULL);
  216. SAVE_PROFILE_INTERVAL(ipc_cld_join_callback);
  217. return 0;
  218. }
  219. DEFINE_PROFILE_INTERVAL(ipc_send_profile, ipc);
  220. #ifdef PROFILE
  221. int ipc_cld_profile_send (void)
  222. {
  223. IDTYPE dest;
  224. struct shim_ipc_port * port = get_parent_port(&dest);
  225. if (!port)
  226. return -ESRCH;
  227. unsigned long time = GET_PROFILE_INTERVAL();
  228. int nsending = 0;
  229. for (int i = 0 ; i < N_PROFILE ; i++)
  230. switch (PROFILES[i].type) {
  231. case OCCURENCE:
  232. if (atomic_read(&PROFILES[i].val.occurence.count))
  233. nsending++;
  234. break;
  235. case INTERVAL:
  236. if (atomic_read(&PROFILES[i].val.interval.count))
  237. nsending++;
  238. break;
  239. case CATAGORY:
  240. break;
  241. }
  242. struct shim_ipc_msg * msg = create_ipc_msg_on_stack(
  243. IPC_CLD_PROFILE,
  244. sizeof(struct shim_ipc_cld_profile) +
  245. sizeof(struct profile_val) *
  246. nsending, dest);
  247. struct shim_ipc_cld_profile * msgin =
  248. (struct shim_ipc_cld_profile *) &msg->msg;
  249. int nsent = 0;
  250. for (int i = 0 ; i < N_PROFILE && nsent < nsending ; i++)
  251. switch (PROFILES[i].type) {
  252. case OCCURENCE: {
  253. unsigned long count =
  254. atomic_read(&PROFILES[i].val.occurence.count);
  255. if (count) {
  256. msgin->profile[nsent].idx = i + 1;
  257. msgin->profile[nsent].val.occurence.count = count;
  258. debug("send %s: %lu times\n", PROFILES[i].name, count);
  259. nsent++;
  260. }
  261. break;
  262. }
  263. case INTERVAL: {
  264. unsigned long count =
  265. atomic_read(&PROFILES[i].val.interval.count);
  266. if (count) {
  267. msgin->profile[nsent].idx = i + 1;
  268. msgin->profile[nsent].val.interval.count = count;
  269. msgin->profile[nsent].val.interval.time =
  270. atomic_read(&PROFILES[i].val.interval.time);
  271. debug("send %s: %lu times, %lu msec\n", PROFILES[i].name,
  272. count, msgin->profile[nsent].val.interval.time);
  273. nsent++;
  274. }
  275. break;
  276. }
  277. case CATAGORY:
  278. break;
  279. }
  280. msgin->time = time;
  281. msgin->nprofile = nsent;
  282. debug("ipc send to %u: IPC_CLD_PROFILE\n", dest);
  283. int ret = send_ipc_message(msg, port);
  284. put_ipc_port(port);
  285. return ret;
  286. }
  287. int ipc_cld_profile_callback (IPC_CALLBACK_ARGS)
  288. {
  289. struct shim_ipc_cld_profile * msgin =
  290. (struct shim_ipc_cld_profile *) &msg->msg;
  291. debug("ipc callback from %u: IPC_CLD_PROFILE\n", msg->src);
  292. for (int i = 0 ; i < msgin->nprofile ; i++) {
  293. int idx = msgin->profile[i].idx;
  294. if (idx == 0)
  295. break;
  296. idx--;
  297. switch (PROFILES[idx].type) {
  298. case OCCURENCE:
  299. debug("receive %s: %u times\n", PROFILES[idx].name,
  300. msgin->profile[i].val.occurence.count);
  301. atomic_add(msgin->profile[i].val.occurence.count,
  302. &PROFILES[idx].val.occurence.count);
  303. break;
  304. case INTERVAL:
  305. debug("receive %s: %u times, %lu msec\n", PROFILES[idx].name,
  306. msgin->profile[i].val.interval.count,
  307. msgin->profile[i].val.interval.time);
  308. atomic_add(msgin->profile[i].val.interval.count,
  309. &PROFILES[idx].val.interval.count);
  310. atomic_add(msgin->profile[i].val.interval.time,
  311. &PROFILES[idx].val.interval.time);
  312. break;
  313. case CATAGORY:
  314. break;
  315. }
  316. }
  317. SAVE_PROFILE_INTERVAL_SINCE(ipc_send_profile, msgin->time);
  318. return 0;
  319. }
  320. #endif