1 /******************************************************************************* 2 3 Contains API to obtain various information about the running application 4 which are available through system APIs. 5 6 Copyright: 7 Copyright (c) 2009-2017 dunnhumby Germany GmbH. 8 All rights reserved. 9 10 License: 11 Boost Software License Version 1.0. See LICENSE_BOOST.txt for details. 12 Alternatively, this file may be distributed under the terms of the Tango 13 3-Clause BSD License (see LICENSE_BSD.txt for details). 14 15 *******************************************************************************/ 16 17 module ocean.sys.stats.linux.Queriable; 18 19 import ocean.meta.types.Qualifiers; 20 import ocean.sys.ErrnoException; 21 import core.sys.posix.sys.resource; 22 import core.stdc.errno; 23 import core.sys.posix.unistd; 24 25 /******************************************************************************* 26 27 Reusable exception instance. 28 29 *******************************************************************************/ 30 31 private ErrnoException exception; 32 33 /******************************************************************************* 34 35 Gets maximum core size allowed for the process to generate. 36 37 Returns: 38 maximum core size allowed for the process to generate. 39 40 Throws: 41 Reusable ErrnoException instance on error. 42 43 *******************************************************************************/ 44 45 public rlimit maximumProcessCoreSize () 46 { 47 return getLimit(RLIMIT_CORE); 48 } 49 50 /******************************************************************************* 51 52 Gets maximum CPU time in seconds process is allowed to use. 53 54 Returns: 55 maximum CPU time in seconds the process is allowed to use. 56 57 Throws: 58 Reusable ErrnoException instance on error. 59 60 *******************************************************************************/ 61 62 public rlimit maximumProcessCPUTime () 63 { 64 return getLimit(RLIMIT_CPU); 65 } 66 67 /******************************************************************************* 68 69 Gets maximum allowed stack size for the process. 70 71 Returns: 72 maximum allowed stack size. 73 74 Throws: 75 Reusable ErrnoException instance on error. 76 77 *******************************************************************************/ 78 79 public rlimit maximumProcessStackSize () 80 { 81 return getLimit(RLIMIT_STACK); 82 } 83 84 /******************************************************************************* 85 86 Gets maximum allowed data segment size for the process. 87 88 Returns: 89 maximum data segment size for the process 90 91 Throws: 92 Reusable ErrnoException instance on error. 93 94 *******************************************************************************/ 95 96 public rlimit maximumProcessDataSize () 97 { 98 return getLimit(RLIMIT_DATA); 99 } 100 101 /******************************************************************************* 102 103 Gets maximum file size allowed for the process to create 104 105 Returns: 106 maximum file size allowed for the process to create 107 108 Throws: 109 Reusable ErrnoException instance on error. 110 111 *******************************************************************************/ 112 113 public rlimit maximumProcessFileSize () 114 { 115 return getLimit(RLIMIT_FSIZE); 116 } 117 118 /******************************************************************************* 119 120 Gets maximum file descriptors allowed for the process to have open 121 122 Returns: 123 maximum file descriptors allowed for the process to have open 124 125 Throws: 126 Reusable ErrnoException instance on error. 127 128 *******************************************************************************/ 129 130 public rlimit maximumProcessNumFiles () 131 { 132 return getLimit(RLIMIT_NOFILE); 133 } 134 135 /******************************************************************************* 136 137 Gets maximum amount of address space allowed for the process to allocate. 138 139 Returns: 140 maximum amount of address space allowed for the process to allocate. 141 142 Throws: 143 Reusable ErrnoException instance on error. 144 145 *******************************************************************************/ 146 147 public rlimit maximumProcessAddressSpace () 148 { 149 return getLimit(RLIMIT_AS); 150 } 151 152 /******************************************************************************* 153 154 Gets the number of clock ticks per second. 155 156 Returns: 157 number of clock ticks per second. 158 159 Throws: 160 Reusable ErrnoException instance on error. 161 162 *******************************************************************************/ 163 164 public long getClockTicksPerSecond () 165 { 166 return getSysconf(_SC_CLK_TCK); 167 } 168 169 /******************************************************************************* 170 171 Gets the page size in bytes 172 173 Returns: 174 Gets the page size in bytes 175 176 Throws: 177 Reusable ErrnoException instance on error. 178 179 *******************************************************************************/ 180 181 public long getPageSize () 182 { 183 return getSysconf(_SC_PAGESIZE); 184 } 185 186 /******************************************************************************* 187 188 Wrapper around getrlimit, checking the return code and 189 throwing an exception if failed. 190 191 Params: 192 limit_type = limit to query 193 194 Returns: 195 rlimit struct describing soft and hard limit 196 197 Throws: 198 Reusable ErrnoException if failed. 199 200 *******************************************************************************/ 201 202 private rlimit getLimit (int limit_type) 203 { 204 rlimit ret; 205 206 if (getrlimit(limit_type, &ret) == -1) 207 { 208 auto saved_errno = .errno; 209 210 if (.exception is null) 211 { 212 .exception = new ErrnoException(); 213 } 214 215 throw .exception.set(saved_errno, "getrlimit"); 216 } 217 218 return ret; 219 } 220 221 /******************************************************************************* 222 223 Wrapper around sysconf(3) call. Converts the error code to the 224 exception. 225 226 Params: 227 name = name of the variable to query 228 229 Returns: 230 value of the system resource identified by name 231 232 Throws: 233 Reusable ErrnoException if failed. 234 235 *******************************************************************************/ 236 237 private long getSysconf (int name) 238 { 239 auto value = sysconf(name); 240 241 if (value == -1) 242 { 243 auto saved_errno = .errno; 244 245 if (.exception is null) 246 { 247 .exception = new ErrnoException(); 248 } 249 250 throw .exception.set(saved_errno, "sysconf"); 251 } 252 253 return value; 254 }