monitor.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /* $Id$ */
  2. static int bs;
  3. static char *bp;
  4. static char *bufp;
  5. static int sc;
  6. static int bufs;
  7. monitor(lowpc, highpc, buffer, bufsize, nfunc)
  8. int (*lowpc)(), (*highpc)();
  9. char *buffer;
  10. {
  11. long scale;
  12. if (lowpc == 0) {
  13. int fd;
  14. profil((char *) 0, 0, 0, 0);
  15. if ((fd = creat("mon.out", 0666)) < 0 || !bp) return;
  16. write(fd, bp, (int) bs);
  17. close(fd);
  18. return;
  19. }
  20. bs = bufsize << 1;
  21. bp = buffer;
  22. *(char **) buffer = (char *) lowpc;
  23. buffer += sizeof(char *);
  24. *(char **) buffer = (char *) highpc;
  25. buffer += sizeof(char *);
  26. *(int *) buffer = nfunc;
  27. buffer += sizeof(int);
  28. buffer += (sizeof (char *) + sizeof(long)) * nfunc;
  29. bufsize -= ((sizeof (char *) + sizeof(long)) * nfunc + 2 * sizeof(char *) + sizeof(int)) >> 1;
  30. if (bufsize < 0) return;
  31. scale = ((char *) highpc - (char *) lowpc) >> 1;
  32. if (bufsize < scale)
  33. scale = ((long) bufsize << 15) / scale;
  34. else scale = 0x8000;
  35. bufp = buffer;
  36. sc = scale << 1;
  37. bufs = bufsize << 1;
  38. profil(buffer, bufs, lowpc, sc);
  39. }
  40. moncontrol(mode)
  41. {
  42. profil(bufp, bufs, *(char **) bp, !mode ? 0 : sc);
  43. }
  44. #define NCOUNTS 300
  45. monstartup(lowpc, highpc)
  46. int (*lowpc)(), (*highpc)();
  47. {
  48. int sz = (((char *) highpc - (char *) lowpc + 7) & ~7);
  49. char *s, *sbrk();
  50. sz += NCOUNTS * (sizeof(long) + sizeof(char *)) + 2 * sizeof(char *) + sizeof(int);
  51. s = sbrk(sz);
  52. if ((int) s == -1) return;
  53. monitor(lowpc, highpc, s, sz >> 1, NCOUNTS);
  54. }