pc.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "all.h"
  10. #include "io.h"
  11. Mconf mconf;
  12. static void
  13. mconfinit(void)
  14. {
  15. int nf, pgsize = 0;
  16. uint32_t size, userpgs = 0, userused = 0;
  17. char *ln, *sl;
  18. char *fields[2];
  19. Biobuf *bp;
  20. Mbank *mbp;
  21. size = 64*MB;
  22. bp = Bopen("#c/swap", OREAD);
  23. if (bp != nil) {
  24. while ((ln = Brdline(bp, '\n')) != nil) {
  25. ln[Blinelen(bp)-1] = '\0';
  26. nf = tokenize(ln, fields, nelem(fields));
  27. if (nf != 2)
  28. continue;
  29. if (strcmp(fields[1], "pagesize") == 0)
  30. pgsize = atoi(fields[0]);
  31. else if (strcmp(fields[1], "user") == 0) {
  32. sl = strchr(fields[0], '/');
  33. if (sl == nil)
  34. continue;
  35. userpgs = atol(sl+1);
  36. userused = atol(fields[0]);
  37. }
  38. }
  39. Bterm(bp);
  40. if (pgsize > 0 && userpgs > 0)
  41. size = (((userpgs - userused)*3LL)/4)*pgsize;
  42. }
  43. mconf.memsize = size;
  44. mbp = mconf.bank;
  45. mbp->base = 0x10000000; /* fake addresses */
  46. mbp->limit = mbp->base + size;
  47. mbp++;
  48. mconf.nbank = mbp - mconf.bank;
  49. }
  50. uint32_t
  51. meminit(void)
  52. {
  53. conf.nmach = 1;
  54. mconfinit();
  55. return mconf.memsize;
  56. }
  57. /*
  58. * based on libthread's threadsetname, but drags in less library code.
  59. * actually just sets the arguments displayed.
  60. */
  61. void
  62. procsetname(char *fmt, ...)
  63. {
  64. int fd;
  65. char *cmdname;
  66. char buf[128];
  67. va_list arg;
  68. va_start(arg, fmt);
  69. cmdname = vsmprint(fmt, arg);
  70. va_end(arg);
  71. if (cmdname == nil)
  72. return;
  73. snprint(buf, sizeof buf, "#p/%d/args", getpid());
  74. if((fd = open(buf, OWRITE)) >= 0){
  75. write(fd, cmdname, strlen(cmdname)+1);
  76. close(fd);
  77. }
  78. free(cmdname);
  79. }
  80. void
  81. newproc(void (*f)(void *), void *arg, char *text)
  82. {
  83. int kid = rfork(RFPROC|RFMEM|RFNOWAIT);
  84. if (kid < 0)
  85. sysfatal("can't fork: %r");
  86. if (kid == 0) {
  87. procsetname("%s", text);
  88. (*f)(arg);
  89. exits("child returned");
  90. }
  91. }