sys_resource.odin 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #+build linux, darwin, netbsd, openbsd, freebsd, haiku
  2. package posix
  3. import "core:c"
  4. when ODIN_OS == .Darwin {
  5. foreign import lib "system:System"
  6. } else {
  7. foreign import lib "system:c"
  8. }
  9. // sys/resource.h - definitions XSI resource operations
  10. foreign lib {
  11. /*
  12. Gets the nice value of the process, process group or user given.
  13. Note that a nice value can be -1, so checking for an error would mean clearing errno, doing the
  14. call and then checking that this returns -1 and it has an errno.
  15. Returns: -1 (setting errno) on failure, the value otherwise
  16. Example:
  17. pid := posix.getpid()
  18. posix.set_errno(.NONE)
  19. prio := posix.getpriority(.PROCESS, pid)
  20. if err := posix.errno(); prio == -1 && err != .NONE {
  21. // Handle error...
  22. }
  23. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/getpriority.html ]]
  24. */
  25. getpriority :: proc(which: Which_Prio, who: id_t) -> c.int ---
  26. /*
  27. Sets the nice value of the process, process group or user given.
  28. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/getpriority.html ]]
  29. */
  30. setpriority :: proc(which: Which_Prio, who: id_t, value: c.int) -> result ---
  31. /*
  32. Get a resource limit.
  33. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/getrlimit.html ]]
  34. */
  35. getrlimit :: proc(resource: Resource, rlp: ^rlimit) -> result ---
  36. /*
  37. Set a resource limit.
  38. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/getrlimit.html ]]
  39. */
  40. setrlimit :: proc(resource: Resource, rlp: ^rlimit) -> result ---
  41. /*
  42. Get resource usage.
  43. [[ More; https://pubs.opengroup.org/onlinepubs/9699919799/functions/getrusage.html ]]
  44. */
  45. @(link_name=LGETRUSAGE)
  46. getrusage :: proc(who: Which_Usage, rusage: ^rusage) -> result ---
  47. }
  48. Which_Prio :: enum c.int {
  49. PROCESS = PRIO_PROCESS,
  50. PGRP = PRIO_PGRP,
  51. USER = PRIO_USER,
  52. }
  53. Which_Usage :: enum c.int {
  54. SELF = RUSAGE_SELF,
  55. CHILDREN = RUSAGE_CHILDREN,
  56. }
  57. Resource :: enum c.int {
  58. // Maximum byte size of a core file that may be created by a process.
  59. CORE = RLIMIT_CORE,
  60. // Maximum amount of CPU time, in seconds, used by a process.
  61. CPU = RLIMIT_CPU,
  62. // Maximum size of data segment of the process, in bytes.
  63. DATA = RLIMIT_DATA,
  64. // Maximum size of a file, in bytes, that may be created by a process.
  65. FSIZE = RLIMIT_FSIZE,
  66. // A number one greater than the maximum value that the system may assign to a newly-created descriptor.
  67. NOFILE = RLIMIT_NOFILE,
  68. // The maximum size of the initial thread's stack, in bytes.
  69. STACK = RLIMIT_STACK,
  70. // Maximum size of total available memory of the process, in bytes.
  71. AS = RLIMIT_AS,
  72. }
  73. when ODIN_OS == .NetBSD {
  74. @(private) LGETRUSAGE :: "__getrusage50"
  75. } else {
  76. @(private) LGETRUSAGE :: "getrusage"
  77. }
  78. when ODIN_OS == .Darwin || ODIN_OS == .FreeBSD || ODIN_OS == .NetBSD || ODIN_OS == .OpenBSD || ODIN_OS == .Linux || ODIN_OS == .Haiku {
  79. PRIO_PROCESS :: 0
  80. PRIO_PGRP :: 1
  81. PRIO_USER :: 2
  82. when ODIN_OS == .Haiku {
  83. rlim_t :: distinct c.ulong
  84. } else {
  85. rlim_t :: distinct c.uint64_t
  86. }
  87. when ODIN_OS == .Haiku {
  88. RLIM_INFINITY :: rlim_t(0xFFFFFFFF)
  89. } else when ODIN_OS == .Linux {
  90. RLIM_INFINITY :: ~rlim_t(0)
  91. } else {
  92. RLIM_INFINITY :: (rlim_t(1) << 63) - 1
  93. }
  94. RLIM_SAVED_MAX :: RLIM_INFINITY
  95. RLIM_SAVED_CUR :: RLIM_INFINITY
  96. RUSAGE_SELF :: 0
  97. RUSAGE_CHILDREN :: -1
  98. rlimit :: struct {
  99. rlim_cur: rlim_t, /* [PSX] the current (soft) limit */
  100. rlim_max: rlim_t, /* [PSX] the hard limit */
  101. }
  102. rusage :: struct {
  103. ru_utime: timeval, /* [PSX] user time used */
  104. ru_stime: timeval, /* [PSX] system time used */
  105. // Informational aliases for source compatibility with programs
  106. // that need more information than that provided by standards,
  107. // and which do not mind being OS-dependent.
  108. ru_maxrss: c.long, /* max resident set size (PL) */
  109. ru_ixrss: c.long, /* integral shared memory size (NU) */
  110. ru_idrss: c.long, /* integral unshared data (NU) */
  111. ru_isrss: c.long, /* integral unshared stack (NU) */
  112. ru_minflt: c.long, /* page reclaims (NU) */
  113. ru_majflt: c.long, /* page faults (NU) */
  114. ru_nswap: c.long, /* swaps (NU) */
  115. ru_inblock: c.long, /* block input operations (atomic) */
  116. ru_outblock: c.long, /* block output operations (atomic) */
  117. ru_msgsnd: c.long, /* messages sent (atomic) */
  118. ru_msgrcv: c.long, /* messages received (atomic) */
  119. ru_nsignals: c.long, /* signals received (atomic) */
  120. ru_nvcsw: c.long, /* voluntary context switches (atomic) */
  121. ru_nivcsw: c.long, /* involuntary " */
  122. }
  123. when ODIN_OS == .Haiku {
  124. RLIMIT_CORE :: 0
  125. RLIMIT_CPU :: 1
  126. RLIMIT_DATA :: 2
  127. RLIMIT_FSIZE :: 3
  128. RLIMIT_NOFILE :: 4
  129. RLIMIT_STACK :: 5
  130. RLIMIT_AS :: 6
  131. } else {
  132. RLIMIT_CORE :: 4
  133. RLIMIT_CPU :: 0
  134. RLIMIT_DATA :: 2
  135. RLIMIT_FSIZE :: 1
  136. RLIMIT_NOFILE :: 7 when ODIN_OS == .Linux else 8
  137. RLIMIT_STACK :: 3
  138. when ODIN_OS == .Linux {
  139. RLIMIT_AS :: 9
  140. } else when ODIN_OS == .Darwin || ODIN_OS == .OpenBSD {
  141. RLIMIT_AS :: 5
  142. } else {
  143. RLIMIT_AS :: 10
  144. }
  145. }
  146. }