shim_ipc_child.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 tid, unsigned int exitcode,
  31. 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. int remove_child_thread (IDTYPE vmid, unsigned int exitcode)
  82. {
  83. assert(vmid != cur_process.vmid);
  84. struct thread_info {
  85. IDTYPE vmid;
  86. unsigned int exitcode;
  87. };
  88. 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->is_alive = false;
  96. DkEventSet(thread->exit_event);
  97. }
  98. return 1;
  99. }
  100. return 0;
  101. }
  102. int child_thread_exit (struct shim_thread * thread, void * arg,
  103. bool * unlocked)
  104. {
  105. struct thread_info * info = (struct thread_info *) arg;
  106. if (thread->vmid == info->vmid) {
  107. if (thread->is_alive) {
  108. thread->exit_code = -info->exitcode;
  109. thread_exit(thread, false);
  110. }
  111. return 1;
  112. }
  113. return 0;
  114. }
  115. struct thread_info info = { .vmid = vmid, .exitcode = exitcode };
  116. int nkilled = 0, ret;
  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 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->tid = tid;
  157. msgin->exitcode = exitcode;
  158. #ifdef PROFILE
  159. msgin->time = send_time;
  160. #endif
  161. debug("ipc broadcast: IPC_CLD_EXIT(%u, %d)\n", tid, exitcode);
  162. ret = broadcast_ipc(msg, NULL, 0, IPC_PORT_DIRPRT|IPC_PORT_DIRCLD);
  163. SAVE_PROFILE_INTERVAL(ipc_cld_exit_send);
  164. return ret;
  165. }
  166. int ipc_cld_exit_callback (IPC_CALLBACK_ARGS)
  167. {
  168. struct shim_ipc_cld_exit * msgin =
  169. (struct shim_ipc_cld_exit *) &msg->msg;
  170. #ifdef PROFILE
  171. unsigned long time = msgin->time;
  172. #else
  173. unsigned long time = 0;
  174. #endif
  175. BEGIN_PROFILE_INTERVAL_SET(time);
  176. SAVE_PROFILE_INTERVAL(ipc_cld_exit_turnaround);
  177. debug("ipc callback from %u: IPC_CLD_EXIT(%u, %d)\n",
  178. msg->src, msgin->tid, msgin->exitcode);
  179. int ret = ipc_thread_exit(msg->src, msgin->tid, msgin->exitcode, time);
  180. SAVE_PROFILE_INTERVAL(ipc_cld_exit_callback);
  181. return ret;
  182. }
  183. DEFINE_PROFILE_INTERVAL(ipc_cld_join_send, ipc);
  184. DEFINE_PROFILE_INTERVAL(ipc_cld_join_callback, ipc);
  185. int ipc_cld_join_send (IDTYPE dest)
  186. {
  187. BEGIN_PROFILE_INTERVAL();
  188. struct shim_ipc_port * port = dest ?
  189. lookup_ipc_port(dest, IPC_PORT_DIRPRT) :
  190. get_parent_port(&dest);
  191. if (!port)
  192. return -ESRCH;
  193. struct shim_ipc_msg * msg =
  194. create_ipc_msg_on_stack(IPC_CLD_JOIN, 0, dest);
  195. debug("ipc send to %u: IPC_CLD_JOIN\n", dest);
  196. int ret = send_ipc_message(msg, port);
  197. add_ipc_port(port, dest, IPC_PORT_DIRPRT, NULL);
  198. put_ipc_port(port);
  199. SAVE_PROFILE_INTERVAL(ipc_cld_join_send);
  200. return ret;
  201. }
  202. int ipc_cld_join_callback (IPC_CALLBACK_ARGS)
  203. {
  204. BEGIN_PROFILE_INTERVAL();
  205. debug("ipc callback from %u: IPC_CLD_JOIN\n", msg->src);
  206. add_ipc_port(port, msg->src, IPC_PORT_DIRCLD, NULL);
  207. SAVE_PROFILE_INTERVAL(ipc_cld_join_callback);
  208. return 0;
  209. }
  210. DEFINE_PROFILE_INTERVAL(ipc_send_profile, ipc);
  211. #ifdef PROFILE
  212. int ipc_cld_profile_send (void)
  213. {
  214. IDTYPE dest;
  215. struct shim_ipc_port * port = get_parent_port(&dest);
  216. if (!port)
  217. return -ESRCH;
  218. unsigned long time = GET_PROFILE_INTERVAL();
  219. int nsending = 0;
  220. for (int i = 0 ; i < N_PROFILE ; i++)
  221. switch (PROFILES[i].type) {
  222. case OCCURENCE:
  223. if (atomic_read(&PROFILES[i].val.occurence.count))
  224. nsending++;
  225. break;
  226. case INTERVAL:
  227. if (atomic_read(&PROFILES[i].val.interval.count))
  228. nsending++;
  229. break;
  230. case CATAGORY:
  231. break;
  232. }
  233. struct shim_ipc_msg * msg = create_ipc_msg_on_stack(
  234. IPC_CLD_PROFILE,
  235. sizeof(struct shim_ipc_cld_profile) +
  236. sizeof(struct profile_val) *
  237. nsending, dest);
  238. struct shim_ipc_cld_profile * msgin =
  239. (struct shim_ipc_cld_profile *) &msg->msg;
  240. int nsent = 0;
  241. for (int i = 0 ; i < N_PROFILE && nsent < nsending ; i++)
  242. switch (PROFILES[i].type) {
  243. case OCCURENCE: {
  244. unsigned long count =
  245. atomic_read(&PROFILES[i].val.occurence.count);
  246. if (count) {
  247. msgin->profile[nsent].idx = i + 1;
  248. msgin->profile[nsent].val.occurence.count = count;
  249. debug("send %s: %lu times\n", PROFILES[i].name, count);
  250. nsent++;
  251. }
  252. break;
  253. }
  254. case INTERVAL: {
  255. unsigned long count =
  256. atomic_read(&PROFILES[i].val.interval.count);
  257. if (count) {
  258. msgin->profile[nsent].idx = i + 1;
  259. msgin->profile[nsent].val.interval.count = count;
  260. msgin->profile[nsent].val.interval.time =
  261. atomic_read(&PROFILES[i].val.interval.time);
  262. debug("send %s: %lu times, %lu msec\n", PROFILES[i].name,
  263. count, msgin->profile[nsent].val.interval.time);
  264. nsent++;
  265. }
  266. break;
  267. }
  268. case CATAGORY:
  269. break;
  270. }
  271. msgin->time = time;
  272. msgin->nprofile = nsent;
  273. debug("ipc send to %u: IPC_CLD_PROFILE\n", dest);
  274. int ret = send_ipc_message(msg, port);
  275. put_ipc_port(port);
  276. return ret;
  277. }
  278. int ipc_cld_profile_callback (IPC_CALLBACK_ARGS)
  279. {
  280. struct shim_ipc_cld_profile * msgin =
  281. (struct shim_ipc_cld_profile *) &msg->msg;
  282. debug("ipc callback from %u: IPC_CLD_PROFILE\n", msg->src);
  283. for (int i = 0 ; i < msgin->nprofile ; i++) {
  284. int idx = msgin->profile[i].idx;
  285. if (idx == 0)
  286. break;
  287. idx--;
  288. switch (PROFILES[idx].type) {
  289. case OCCURENCE:
  290. debug("receive %s: %u times\n", PROFILES[idx].name,
  291. msgin->profile[i].val.occurence.count);
  292. atomic_add(msgin->profile[i].val.occurence.count,
  293. &PROFILES[idx].val.occurence.count);
  294. break;
  295. case INTERVAL:
  296. debug("receive %s: %u times, %lu msec\n", PROFILES[idx].name,
  297. msgin->profile[i].val.interval.count,
  298. msgin->profile[i].val.interval.time);
  299. atomic_add(msgin->profile[i].val.interval.count,
  300. &PROFILES[idx].val.interval.count);
  301. atomic_add(msgin->profile[i].val.interval.time,
  302. &PROFILES[idx].val.interval.time);
  303. break;
  304. case CATAGORY:
  305. break;
  306. }
  307. }
  308. SAVE_PROFILE_INTERVAL_SINCE(ipc_send_profile, msgin->time);
  309. return 0;
  310. }
  311. #endif