shim_ipc_child.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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 OSCAR lab, 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 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 General Public License for more details.
  13. You should have received a copy of the GNU 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 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. #ifdef PROFILE
  44. thread->exit_time = exit_time;
  45. #endif
  46. ret = thread_exit(thread, false);
  47. put_thread(thread);
  48. return ret;
  49. }
  50. struct shim_simple_thread * sthread = __lookup_simple_thread(tid);
  51. if (!sthread) {
  52. sthread = get_new_simple_thread();
  53. sthread->vmid = vmid;
  54. sthread->tid = tid;
  55. add_simple_thread(sthread);
  56. }
  57. sthread->is_alive = 0;
  58. sthread->exit_code = -exitcode;
  59. #ifdef PROFILE
  60. sthread->exit_time = exit_time;
  61. #endif
  62. DkEventSet(sthread->exit_event);
  63. put_simple_thread(sthread);
  64. return 0;
  65. }
  66. void ipc_parent_exit (struct shim_ipc_port * port, IDTYPE vmid,
  67. unsigned int exitcode)
  68. {
  69. debug("ipc port %p of process %u closed suggests parent exiting\n",
  70. port, vmid);
  71. struct shim_ipc_info * parent = NULL;
  72. lock(cur_process.lock);
  73. if (parent && vmid == cur_process.parent->vmid) {
  74. parent = cur_process.parent;
  75. cur_process.parent = NULL;
  76. }
  77. unlock(cur_process.lock);
  78. if (parent)
  79. put_ipc_info(parent);
  80. }
  81. struct thread_info {
  82. IDTYPE vmid;
  83. unsigned int exitcode;
  84. };
  85. static int child_sthread_exit (struct shim_simple_thread * thread, void * arg,
  86. bool * unlocked)
  87. {
  88. struct thread_info * info = (struct thread_info *) arg;
  89. if (thread->vmid == info->vmid) {
  90. if (thread->is_alive) {
  91. thread->exit_code = -info->exitcode;
  92. thread->is_alive = false;
  93. DkEventSet(thread->exit_event);
  94. }
  95. return 1;
  96. }
  97. return 0;
  98. }
  99. static int child_thread_exit (struct shim_thread * thread, void * arg,
  100. bool * unlocked)
  101. {
  102. struct thread_info * info = (struct thread_info *) arg;
  103. if (thread->vmid == info->vmid) {
  104. if (thread->is_alive) {
  105. thread->exit_code = -info->exitcode;
  106. thread_exit(thread, false);
  107. }
  108. return 1;
  109. }
  110. return 0;
  111. }
  112. int remove_child_thread (IDTYPE vmid, unsigned int exitcode)
  113. {
  114. struct thread_info info = { .vmid = vmid, .exitcode = exitcode };
  115. int nkilled = 0, ret;
  116. assert(vmid != cur_process.vmid);
  117. if ((ret = walk_thread_list(&child_thread_exit, &info, false)) > 0)
  118. nkilled += ret;
  119. if ((ret = walk_simple_thread_list(&child_sthread_exit, &info, false)) > 0)
  120. nkilled += ret;
  121. if (!nkilled)
  122. debug("child port closed, no thread exited\n");
  123. return 0;
  124. }
  125. void ipc_child_exit (struct shim_ipc_port * port, IDTYPE vmid,
  126. unsigned int exitcode)
  127. {
  128. debug("ipc port %p of process %u closed suggests child exiting\n",
  129. port, vmid);
  130. remove_child_thread(vmid, 0);
  131. }
  132. static struct shim_ipc_port * get_parent_port (IDTYPE * dest)
  133. {
  134. struct shim_ipc_port * port = NULL;
  135. lock(cur_process.lock);
  136. if (cur_process.parent && (port = cur_process.parent->port)) {
  137. get_ipc_port(port);
  138. *dest = cur_process.parent->vmid;
  139. }
  140. unlock(cur_process.lock);
  141. return port;
  142. }
  143. DEFINE_PROFILE_INTERVAL(ipc_cld_exit_turnaround, ipc);
  144. DEFINE_PROFILE_INTERVAL(ipc_cld_exit_send, ipc);
  145. DEFINE_PROFILE_INTERVAL(ipc_cld_exit_callback, ipc);
  146. int ipc_cld_exit_send (IDTYPE ppid, IDTYPE tid, unsigned int exitcode)
  147. {
  148. unsigned long send_time = GET_PROFILE_INTERVAL();
  149. BEGIN_PROFILE_INTERVAL_SET(send_time);
  150. int ret = 0;
  151. struct shim_ipc_msg * msg =
  152. create_ipc_msg_on_stack(IPC_CLD_EXIT,
  153. sizeof(struct shim_ipc_cld_exit), 0);
  154. struct shim_ipc_cld_exit * msgin =
  155. (struct shim_ipc_cld_exit *) &msg->msg;
  156. msgin->ppid = ppid;
  157. msgin->tid = tid;
  158. msgin->exitcode = exitcode;
  159. #ifdef PROFILE
  160. msgin->time = send_time;
  161. #endif
  162. debug("ipc broadcast: IPC_CLD_EXIT(%u, %u, %d)\n", ppid, tid, exitcode);
  163. ret = broadcast_ipc(msg, NULL, 0, IPC_PORT_DIRPRT|IPC_PORT_DIRCLD);
  164. SAVE_PROFILE_INTERVAL(ipc_cld_exit_send);
  165. return ret;
  166. }
  167. int ipc_cld_exit_callback (IPC_CALLBACK_ARGS)
  168. {
  169. struct shim_ipc_cld_exit * msgin =
  170. (struct shim_ipc_cld_exit *) &msg->msg;
  171. #ifdef PROFILE
  172. unsigned long time = msgin->time;
  173. #else
  174. unsigned long time = 0;
  175. #endif
  176. BEGIN_PROFILE_INTERVAL_SET(time);
  177. SAVE_PROFILE_INTERVAL(ipc_cld_exit_turnaround);
  178. debug("ipc callback from %u: IPC_CLD_EXIT(%u, %u, %d)\n",
  179. msg->src, msgin->ppid, msgin->tid, msgin->exitcode);
  180. int ret = ipc_thread_exit(msg->src, msgin->ppid, msgin->tid,
  181. msgin->exitcode, time);
  182. SAVE_PROFILE_INTERVAL(ipc_cld_exit_callback);
  183. return ret;
  184. }
  185. DEFINE_PROFILE_INTERVAL(ipc_cld_join_send, ipc);
  186. DEFINE_PROFILE_INTERVAL(ipc_cld_join_callback, ipc);
  187. int ipc_cld_join_send (IDTYPE dest)
  188. {
  189. BEGIN_PROFILE_INTERVAL();
  190. struct shim_ipc_port * port = dest ?
  191. lookup_ipc_port(dest, IPC_PORT_DIRPRT) :
  192. get_parent_port(&dest);
  193. if (!port)
  194. return -ESRCH;
  195. struct shim_ipc_msg * msg =
  196. create_ipc_msg_on_stack(IPC_CLD_JOIN, 0, dest);
  197. debug("ipc send to %u: IPC_CLD_JOIN\n", dest);
  198. int ret = send_ipc_message(msg, port);
  199. add_ipc_port(port, dest, IPC_PORT_DIRPRT, NULL);
  200. put_ipc_port(port);
  201. SAVE_PROFILE_INTERVAL(ipc_cld_join_send);
  202. return ret;
  203. }
  204. int ipc_cld_join_callback (IPC_CALLBACK_ARGS)
  205. {
  206. BEGIN_PROFILE_INTERVAL();
  207. debug("ipc callback from %u: IPC_CLD_JOIN\n", msg->src);
  208. add_ipc_port(port, msg->src, IPC_PORT_DIRCLD, NULL);
  209. SAVE_PROFILE_INTERVAL(ipc_cld_join_callback);
  210. return 0;
  211. }
  212. DEFINE_PROFILE_INTERVAL(ipc_send_profile, ipc);
  213. #ifdef PROFILE
  214. int ipc_cld_profile_send (void)
  215. {
  216. IDTYPE dest;
  217. struct shim_ipc_port * port = get_parent_port(&dest);
  218. if (!port)
  219. return -ESRCH;
  220. unsigned long time = GET_PROFILE_INTERVAL();
  221. int nsending = 0;
  222. for (int i = 0 ; i < N_PROFILE ; i++)
  223. switch (PROFILES[i].type) {
  224. case OCCURENCE:
  225. if (atomic_read(&PROFILES[i].val.occurence.count))
  226. nsending++;
  227. break;
  228. case INTERVAL:
  229. if (atomic_read(&PROFILES[i].val.interval.count))
  230. nsending++;
  231. break;
  232. case CATAGORY:
  233. break;
  234. }
  235. struct shim_ipc_msg * msg = create_ipc_msg_on_stack(
  236. IPC_CLD_PROFILE,
  237. sizeof(struct shim_ipc_cld_profile) +
  238. sizeof(struct profile_val) *
  239. nsending, dest);
  240. struct shim_ipc_cld_profile * msgin =
  241. (struct shim_ipc_cld_profile *) &msg->msg;
  242. int nsent = 0;
  243. for (int i = 0 ; i < N_PROFILE && nsent < nsending ; i++)
  244. switch (PROFILES[i].type) {
  245. case OCCURENCE: {
  246. unsigned long count =
  247. atomic_read(&PROFILES[i].val.occurence.count);
  248. if (count) {
  249. msgin->profile[nsent].idx = i + 1;
  250. msgin->profile[nsent].val.occurence.count = count;
  251. debug("send %s: %lu times\n", PROFILES[i].name, count);
  252. nsent++;
  253. }
  254. break;
  255. }
  256. case INTERVAL: {
  257. unsigned long count =
  258. atomic_read(&PROFILES[i].val.interval.count);
  259. if (count) {
  260. msgin->profile[nsent].idx = i + 1;
  261. msgin->profile[nsent].val.interval.count = count;
  262. msgin->profile[nsent].val.interval.time =
  263. atomic_read(&PROFILES[i].val.interval.time);
  264. debug("send %s: %lu times, %lu msec\n", PROFILES[i].name,
  265. count, msgin->profile[nsent].val.interval.time);
  266. nsent++;
  267. }
  268. break;
  269. }
  270. case CATAGORY:
  271. break;
  272. }
  273. msgin->time = time;
  274. msgin->nprofile = nsent;
  275. debug("ipc send to %u: IPC_CLD_PROFILE\n", dest);
  276. int ret = send_ipc_message(msg, port);
  277. put_ipc_port(port);
  278. return ret;
  279. }
  280. int ipc_cld_profile_callback (IPC_CALLBACK_ARGS)
  281. {
  282. struct shim_ipc_cld_profile * msgin =
  283. (struct shim_ipc_cld_profile *) &msg->msg;
  284. debug("ipc callback from %u: IPC_CLD_PROFILE\n", msg->src);
  285. for (int i = 0 ; i < msgin->nprofile ; i++) {
  286. int idx = msgin->profile[i].idx;
  287. if (idx == 0)
  288. break;
  289. idx--;
  290. switch (PROFILES[idx].type) {
  291. case OCCURENCE:
  292. debug("receive %s: %u times\n", PROFILES[idx].name,
  293. msgin->profile[i].val.occurence.count);
  294. atomic_add(msgin->profile[i].val.occurence.count,
  295. &PROFILES[idx].val.occurence.count);
  296. break;
  297. case INTERVAL:
  298. debug("receive %s: %u times, %lu msec\n", PROFILES[idx].name,
  299. msgin->profile[i].val.interval.count,
  300. msgin->profile[i].val.interval.time);
  301. atomic_add(msgin->profile[i].val.interval.count,
  302. &PROFILES[idx].val.interval.count);
  303. atomic_add(msgin->profile[i].val.interval.time,
  304. &PROFILES[idx].val.interval.time);
  305. break;
  306. case CATAGORY:
  307. break;
  308. }
  309. }
  310. SAVE_PROFILE_INTERVAL_SINCE(ipc_send_profile, msgin->time);
  311. return 0;
  312. }
  313. #endif