sys_resource.odin 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #+build linux, darwin, netbsd, openbsd, freebsd
  2. package posix
  3. import "core:c"
  4. when ODIN_OS == .Darwin {
  5. foreign import lib "system:System.framework"
  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 {
  79. PRIO_PROCESS :: 0
  80. PRIO_PGRP :: 1
  81. PRIO_USER :: 2
  82. rlim_t :: distinct c.uint64_t
  83. RLIM_INFINITY :: ~rlim_t(0) when ODIN_OS == .Linux else (rlim_t(1) << 63) - 1
  84. RLIM_SAVED_MAX :: RLIM_INFINITY
  85. RLIM_SAVED_CUR :: RLIM_INFINITY
  86. RUSAGE_SELF :: 0
  87. RUSAGE_CHILDREN :: -1
  88. rlimit :: struct {
  89. rlim_cur: rlim_t, /* [PSX] the current (soft) limit */
  90. rlim_max: rlim_t, /* [PSX] the hard limit */
  91. }
  92. rusage :: struct {
  93. ru_utime: timeval, /* [PSX] user time used */
  94. ru_stime: timeval, /* [PSX] system time used */
  95. // Informational aliases for source compatibility with programs
  96. // that need more information than that provided by standards,
  97. // and which do not mind being OS-dependent.
  98. ru_maxrss: c.long, /* max resident set size (PL) */
  99. ru_ixrss: c.long, /* integral shared memory size (NU) */
  100. ru_idrss: c.long, /* integral unshared data (NU) */
  101. ru_isrss: c.long, /* integral unshared stack (NU) */
  102. ru_minflt: c.long, /* page reclaims (NU) */
  103. ru_majflt: c.long, /* page faults (NU) */
  104. ru_nswap: c.long, /* swaps (NU) */
  105. ru_inblock: c.long, /* block input operations (atomic) */
  106. ru_outblock: c.long, /* block output operations (atomic) */
  107. ru_msgsnd: c.long, /* messages sent (atomic) */
  108. ru_msgrcv: c.long, /* messages received (atomic) */
  109. ru_nsignals: c.long, /* signals received (atomic) */
  110. ru_nvcsw: c.long, /* voluntary context switches (atomic) */
  111. ru_nivcsw: c.long, /* involuntary " */
  112. }
  113. RLIMIT_CORE :: 4
  114. RLIMIT_CPU :: 0
  115. RLIMIT_DATA :: 2
  116. RLIMIT_FSIZE :: 1
  117. RLIMIT_NOFILE :: 7 when ODIN_OS == .Linux else 8
  118. RLIMIT_STACK :: 3
  119. when ODIN_OS == .Linux {
  120. RLIMIT_AS :: 9
  121. } else when ODIN_OS == .Darwin || ODIN_OS == .OpenBSD {
  122. RLIMIT_AS :: 5
  123. } else {
  124. RLIMIT_AS :: 10
  125. }
  126. }