sys_resource.odin 4.4 KB

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