LCOV - code coverage report
Current view: top level - src - sys.c (source / functions) Hit Total Coverage
Test: [test only] commit 0f242327d2cc9bd130497f44b6350c924185606a Lines: 190 225 84.4 %
Date: 2022-07-16 23:42:53 Functions: 52 63 82.5 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 60 76 78.9 %

           Branch data     Line data    Source code
       1                 :            : // This file is a part of Julia. License is MIT: https://julialang.org/license
       2                 :            : 
       3                 :            : /*
       4                 :            :   sys.c
       5                 :            :   I/O and operating system utility functions
       6                 :            : */
       7                 :            : #include <sys/stat.h>
       8                 :            : #include <stdlib.h>
       9                 :            : #include <string.h>
      10                 :            : #include <errno.h>
      11                 :            : #include <signal.h>
      12                 :            : #include <fcntl.h>
      13                 :            : 
      14                 :            : #include "julia.h"
      15                 :            : #include "julia_internal.h"
      16                 :            : 
      17                 :            : #ifdef _OS_WINDOWS_
      18                 :            : #include <psapi.h>
      19                 :            : #else
      20                 :            : #include <unistd.h>
      21                 :            : #if !defined(_SC_NPROCESSORS_ONLN) || defined(_OS_FREEBSD_) || defined(_OS_DARWIN_)
      22                 :            : // try secondary location for _SC_NPROCESSORS_ONLN, or for HW_AVAILCPU on BSDs
      23                 :            : #include <sys/sysctl.h>
      24                 :            : #endif
      25                 :            : #include <sys/wait.h>
      26                 :            : #include <sys/ptrace.h>
      27                 :            : #include <sys/mman.h>
      28                 :            : #include <dlfcn.h>
      29                 :            : #include <grp.h>
      30                 :            : 
      31                 :            : // For `struct termios`
      32                 :            : #include <termios.h>
      33                 :            : #endif
      34                 :            : 
      35                 :            : #ifndef _OS_WINDOWS_
      36                 :            : // for getrusage
      37                 :            : #include <sys/types.h>
      38                 :            : #include <sys/time.h>
      39                 :            : #include <sys/resource.h>
      40                 :            : #endif
      41                 :            : 
      42                 :            : #ifdef __APPLE__
      43                 :            : #include <mach-o/dyld.h>
      44                 :            : #include <mach-o/nlist.h>
      45                 :            : #include <sys/types.h> // for jl_raise_debugger
      46                 :            : #elif !defined(_OS_WINDOWS_)
      47                 :            : #include <link.h>
      48                 :            : #endif
      49                 :            : 
      50                 :            : #ifdef __SSE__
      51                 :            : #include <xmmintrin.h>
      52                 :            : #endif
      53                 :            : 
      54                 :            : #ifdef _COMPILER_MSAN_ENABLED_
      55                 :            : #include <sanitizer/msan_interface.h>
      56                 :            : #endif
      57                 :            : 
      58                 :            : #include "julia_assert.h"
      59                 :            : 
      60                 :            : #ifdef __cplusplus
      61                 :            : extern "C" {
      62                 :            : #endif
      63                 :            : 
      64                 :          0 : JL_DLLEXPORT int jl_sizeof_off_t(void) { return sizeof(off_t); }
      65                 :            : #ifndef _OS_WINDOWS_
      66                 :          1 : JL_DLLEXPORT int jl_sizeof_mode_t(void) { return sizeof(mode_t); }
      67                 :         48 : JL_DLLEXPORT int jl_ftruncate(int fd, int64_t length)
      68                 :            : {
      69                 :         48 :     return ftruncate(fd, (off_t)length);
      70                 :            : }
      71                 :    8719360 : JL_DLLEXPORT int64_t jl_lseek(int fd, int64_t offset, int whence)
      72                 :            : {
      73                 :    8719360 :     return lseek(fd, (off_t)offset, whence);
      74                 :            : }
      75                 :          0 : JL_DLLEXPORT ssize_t jl_pwrite(int fd, const void *buf, size_t count, int64_t offset)
      76                 :            : {
      77                 :          0 :     return pwrite(fd, buf, count, (off_t)offset);
      78                 :            : }
      79                 :        273 : JL_DLLEXPORT void *jl_mmap(void *addr, size_t length, int prot, int flags,
      80                 :            :                            int fd, int64_t offset)
      81                 :            : {
      82                 :        273 :     return mmap(addr, length, prot, flags, fd, (off_t)offset);
      83                 :            : }
      84                 :            : #else
      85                 :            : JL_DLLEXPORT int64_t jl_lseek(HANDLE fd, int64_t offset, int whence)
      86                 :            : {
      87                 :            :     LARGE_INTEGER tell;
      88                 :            :     tell.QuadPart = offset;
      89                 :            :     if (SetFilePointerEx(fd, tell, &tell, whence) == 0)
      90                 :            :         return -1;
      91                 :            :     return tell.QuadPart;
      92                 :            : }
      93                 :            : #endif
      94                 :          1 : JL_DLLEXPORT int jl_sizeof_ios_t(void) { return sizeof(ios_t); }
      95                 :            : 
      96                 :        471 : JL_DLLEXPORT long jl_ios_fd(ios_t *s) { return s->fd; }
      97                 :            : 
      98                 :         11 : JL_DLLEXPORT int32_t jl_nb_available(ios_t *s)
      99                 :            : {
     100                 :         11 :     return (int32_t)(s->size - s->bpos);
     101                 :            : }
     102                 :            : 
     103                 :            : // --- dir/file stuff ---
     104                 :            : 
     105                 :          0 : JL_DLLEXPORT int jl_sizeof_uv_fs_t(void) { return sizeof(uv_fs_t); }
     106                 :     103704 : JL_DLLEXPORT char *jl_uv_fs_t_ptr(uv_fs_t *req) { return (char*)req->ptr; }
     107                 :        488 : JL_DLLEXPORT char *jl_uv_fs_t_path(uv_fs_t *req) { return (char*)req->path; }
     108                 :            : 
     109                 :            : // --- stat ---
     110                 :   10920000 : JL_DLLEXPORT int jl_sizeof_stat(void) { return sizeof(uv_stat_t); }
     111                 :            : 
     112                 :    2133560 : JL_DLLEXPORT int32_t jl_stat(const char *path, char *statbuf) JL_NOTSAFEPOINT
     113                 :            : {
     114                 :            :     uv_fs_t req;
     115                 :            :     int ret;
     116                 :            : 
     117                 :            :     // Ideally one would use the statbuf for the storage in req, but
     118                 :            :     // it's not clear that this is possible using libuv
     119                 :    2133560 :     ret = uv_fs_stat(unused_uv_loop_arg, &req, path, NULL);
     120         [ +  + ]:    2133560 :     if (ret == 0)
     121                 :    1261730 :         memcpy(statbuf, req.ptr, sizeof(uv_stat_t));
     122                 :    2133560 :     uv_fs_req_cleanup(&req);
     123                 :    2133560 :     return ret;
     124                 :            : }
     125                 :            : 
     126                 :      59661 : JL_DLLEXPORT int32_t jl_lstat(const char *path, char *statbuf)
     127                 :            : {
     128                 :            :     uv_fs_t req;
     129                 :            :     int ret;
     130                 :            : 
     131                 :      59661 :     ret = uv_fs_lstat(unused_uv_loop_arg, &req, path, NULL);
     132         [ +  + ]:      59661 :     if (ret == 0)
     133                 :      50965 :         memcpy(statbuf, req.ptr, sizeof(uv_stat_t));
     134                 :      59661 :     uv_fs_req_cleanup(&req);
     135                 :      59661 :     return ret;
     136                 :            : }
     137                 :            : 
     138                 :    8726780 : JL_DLLEXPORT int32_t jl_fstat(uv_os_fd_t fd, char *statbuf)
     139                 :            : {
     140                 :            :     uv_fs_t req;
     141                 :            :     int ret;
     142                 :            : 
     143                 :    8726780 :     ret = uv_fs_fstat(unused_uv_loop_arg, &req, fd, NULL);
     144         [ +  + ]:    8726780 :     if (ret == 0)
     145                 :    8726780 :         memcpy(statbuf, req.ptr, sizeof(uv_stat_t));
     146                 :    8726780 :     uv_fs_req_cleanup(&req);
     147                 :    8726780 :     return ret;
     148                 :            : }
     149                 :            : 
     150                 :   10920000 : JL_DLLEXPORT unsigned int jl_stat_dev(char *statbuf)
     151                 :            : {
     152                 :   10920000 :     return ((uv_stat_t*)statbuf)->st_dev;
     153                 :            : }
     154                 :            : 
     155                 :   10920000 : JL_DLLEXPORT unsigned int jl_stat_ino(char *statbuf)
     156                 :            : {
     157                 :   10920000 :     return ((uv_stat_t*)statbuf)->st_ino;
     158                 :            : }
     159                 :            : 
     160                 :   10920000 : JL_DLLEXPORT unsigned int jl_stat_mode(char *statbuf)
     161                 :            : {
     162                 :   10920000 :     return ((uv_stat_t*)statbuf)->st_mode;
     163                 :            : }
     164                 :            : 
     165                 :   10920000 : JL_DLLEXPORT unsigned int jl_stat_nlink(char *statbuf)
     166                 :            : {
     167                 :   10920000 :     return ((uv_stat_t*)statbuf)->st_nlink;
     168                 :            : }
     169                 :            : 
     170                 :   10920000 : JL_DLLEXPORT unsigned int jl_stat_uid(char *statbuf)
     171                 :            : {
     172                 :   10920000 :     return ((uv_stat_t*)statbuf)->st_uid;
     173                 :            : }
     174                 :            : 
     175                 :   10920000 : JL_DLLEXPORT unsigned int jl_stat_gid(char *statbuf)
     176                 :            : {
     177                 :   10920000 :     return ((uv_stat_t*)statbuf)->st_gid;
     178                 :            : }
     179                 :            : 
     180                 :   10920000 : JL_DLLEXPORT unsigned int jl_stat_rdev(char *statbuf)
     181                 :            : {
     182                 :   10920000 :     return ((uv_stat_t*)statbuf)->st_rdev;
     183                 :            : }
     184                 :            : 
     185                 :   10920000 : JL_DLLEXPORT uint64_t jl_stat_size(char *statbuf)
     186                 :            : {
     187                 :   10920000 :     return ((uv_stat_t*)statbuf)->st_size;
     188                 :            : }
     189                 :            : 
     190                 :   10920000 : JL_DLLEXPORT uint64_t jl_stat_blksize(char *statbuf)
     191                 :            : {
     192                 :   10920000 :     return ((uv_stat_t*)statbuf)->st_blksize;
     193                 :            : }
     194                 :            : 
     195                 :   10920000 : JL_DLLEXPORT uint64_t jl_stat_blocks(char *statbuf)
     196                 :            : {
     197                 :   10920000 :     return ((uv_stat_t*)statbuf)->st_blocks;
     198                 :            : }
     199                 :            : 
     200                 :            : /*
     201                 :            : // atime is stupid, let's not support it
     202                 :            : JL_DLLEXPORT double jl_stat_atime(char *statbuf)
     203                 :            : {
     204                 :            :   uv_stat_t *s;
     205                 :            :   s = (uv_stat_t*)statbuf;
     206                 :            :   return (double)s->st_atim.tv_sec + (double)s->st_atim.tv_nsec * 1e-9;
     207                 :            : }
     208                 :            : */
     209                 :            : 
     210                 :   10920000 : JL_DLLEXPORT double jl_stat_mtime(char *statbuf)
     211                 :            : {
     212                 :            :     uv_stat_t *s;
     213                 :   10920000 :     s = (uv_stat_t*)statbuf;
     214                 :   10920000 :     return (double)s->st_mtim.tv_sec + (double)s->st_mtim.tv_nsec * 1e-9;
     215                 :            : }
     216                 :            : 
     217                 :   10920000 : JL_DLLEXPORT double jl_stat_ctime(char *statbuf)
     218                 :            : {
     219                 :            :     uv_stat_t *s;
     220                 :   10920000 :     s = (uv_stat_t*)statbuf;
     221                 :   10920000 :     return (double)s->st_ctim.tv_sec + (double)s->st_ctim.tv_nsec * 1e-9;
     222                 :            : }
     223                 :            : 
     224                 :          1 : JL_DLLEXPORT unsigned long jl_getuid(void)
     225                 :            : {
     226                 :            : #ifdef _OS_WINDOWS_
     227                 :            :     return -1;
     228                 :            : #else
     229                 :          1 :     return getuid();
     230                 :            : #endif
     231                 :            : }
     232                 :            : 
     233                 :          3 : JL_DLLEXPORT unsigned long jl_geteuid(void)
     234                 :            : {
     235                 :            : #ifdef _OS_WINDOWS_
     236                 :            :     return -1;
     237                 :            : #else
     238                 :          3 :     return geteuid();
     239                 :            : #endif
     240                 :            : }
     241                 :            : 
     242                 :            : // --- buffer manipulation ---
     243                 :            : 
     244                 :    2205650 : JL_DLLEXPORT jl_array_t *jl_take_buffer(ios_t *s)
     245                 :            : {
     246                 :            :     size_t n;
     247                 :            :     jl_array_t *a;
     248         [ +  + ]:    2205650 :     if (s->buf == &s->local[0]) {
     249                 :            :         // small data case. copies, but this can be avoided using the
     250                 :            :         // technique of jl_readuntil below.
     251                 :     173285 :         a = jl_pchar_to_array(s->buf, s->size);
     252                 :     173285 :         ios_trunc(s, 0);
     253                 :            :     }
     254                 :            :     else {
     255                 :    2032370 :         char *b = ios_take_buffer(s, &n);
     256                 :    2032370 :         a = jl_ptr_to_array_1d(jl_array_uint8_type, b, n-1, 1);
     257                 :            :     }
     258                 :    2205650 :     return a;
     259                 :            : }
     260                 :            : 
     261                 :            : // str: if 1 return a string, otherwise return a Vector{UInt8}
     262                 :            : // chomp:
     263                 :            : //   0 - keep delimiter
     264                 :            : //   1 - remove 1 byte delimiter
     265                 :            : //   2 - remove 2 bytes \r\n if present
     266                 :     185836 : JL_DLLEXPORT jl_value_t *jl_readuntil(ios_t *s, uint8_t delim, uint8_t str, uint8_t chomp)
     267                 :            : {
     268                 :            :     jl_array_t *a;
     269                 :            :     // manually inlined common case
     270                 :     185836 :     char *pd = (char*)memchr(s->buf + s->bpos, delim, (size_t)(s->size - s->bpos));
     271         [ +  + ]:     185836 :     if (pd) {
     272                 :     185053 :         size_t n = pd - (s->buf + s->bpos) + 1;
     273                 :     185053 :         size_t nchomp = 0;
     274         [ +  + ]:     185053 :         if (chomp) {
     275         [ +  - ]:     185002 :             nchomp = chomp == 2 ? ios_nchomp(s, n) : 1;
     276                 :            :         }
     277         [ +  - ]:     185053 :         if (str) {
     278                 :     185053 :             jl_value_t *str = jl_pchar_to_string(s->buf + s->bpos, n - nchomp);
     279                 :     185053 :             s->bpos += n;
     280                 :     185053 :             return str;
     281                 :            :         }
     282                 :          0 :         a = jl_alloc_array_1d(jl_array_uint8_type, n - nchomp);
     283                 :          0 :         memcpy(jl_array_data(a), s->buf + s->bpos, n - nchomp);
     284                 :          0 :         s->bpos += n;
     285                 :            :     }
     286                 :            :     else {
     287                 :        783 :         a = jl_alloc_array_1d(jl_array_uint8_type, 80);
     288                 :            :         ios_t dest;
     289                 :        783 :         ios_mem(&dest, 0);
     290                 :        783 :         ios_setbuf(&dest, (char*)a->data, 80, 0);
     291                 :        783 :         size_t n = ios_copyuntil(&dest, s, delim);
     292   [ +  +  +  +  :        783 :         if (chomp && n > 0 && dest.buf[n - 1] == delim) {
                   +  + ]
     293                 :        368 :             n--;
     294   [ +  +  +  -  :        368 :             if (chomp == 2 && n > 0 && dest.buf[n - 1] == '\r') {
                   +  + ]
     295                 :         11 :                 n--;
     296                 :            :             }
     297                 :        368 :             int truncret = ios_trunc(&dest, n); // it should always be possible to truncate dest
     298         [ -  + ]:        368 :             assert(truncret == 0);
     299                 :            :             (void)truncret; // ensure the variable is used to avoid warnings
     300                 :            :         }
     301         [ +  + ]:        783 :         if (dest.buf != a->data) {
     302                 :        405 :             a = jl_take_buffer(&dest);
     303                 :            :         }
     304                 :            :         else {
     305                 :        378 :             a->length = n;
     306                 :        378 :             a->nrows = n;
     307                 :        378 :             ((char*)a->data)[n] = '\0';
     308                 :            :         }
     309         [ +  - ]:        783 :         if (str) {
     310                 :        783 :             JL_GC_PUSH1(&a);
     311                 :        783 :             jl_value_t *st = jl_array_to_string(a);
     312                 :        783 :             JL_GC_POP();
     313                 :        783 :             return st;
     314                 :            :         }
     315                 :            :     }
     316                 :          0 :     return (jl_value_t*)a;
     317                 :            : }
     318                 :            : 
     319                 :      46375 : JL_DLLEXPORT int jl_ios_buffer_n(ios_t *s, const size_t n)
     320                 :            : {
     321                 :            :     size_t space, ret;
     322                 :            :     do {
     323                 :      46375 :         space = (size_t)(s->size - s->bpos);
     324                 :      46375 :         ret = ios_readprep(s, n);
     325   [ +  +  +  + ]:      46375 :         if (space == ret && ret < n)
     326                 :          6 :             return 1;
     327         [ +  + ]:      46369 :     } while (ret < n);
     328                 :      46365 :     return 0;
     329                 :            : }
     330                 :            : 
     331                 :      46365 : JL_DLLEXPORT uint64_t jl_ios_get_nbyte_int(ios_t *s, const size_t n)
     332                 :            : {
     333         [ -  + ]:      46365 :     assert(n <= 8);
     334                 :      46365 :     uint64_t x = 0;
     335                 :      46365 :     uint8_t *buf = (uint8_t*)&s->buf[s->bpos];
     336         [ +  + ]:      46365 :     if (n == 8) {
     337                 :            :         // expecting loop unrolling optimization
     338         [ +  + ]:     291249 :         for (size_t i = 0; i < 8; i++)
     339                 :     258888 :             x |= (uint64_t)buf[i] << (i << 3);
     340                 :            :     }
     341         [ +  + ]:      14004 :     else if (n >= 4) {
     342                 :            :         // expecting loop unrolling optimization
     343         [ +  + ]:      67585 :         for (size_t i = 0; i < 4; i++)
     344                 :      54068 :             x |= (uint64_t)buf[i] << (i << 3);
     345         [ -  + ]:      13517 :         for (size_t i = 4; i < n; i++)
     346                 :          0 :             x |= (uint64_t)buf[i] << (i << 3);
     347                 :            :     }
     348                 :            :     else {
     349         [ +  + ]:       1461 :         for (size_t i = 0; i < n; i++)
     350                 :        974 :             x |= (uint64_t)buf[i] << (i << 3);
     351                 :            :     }
     352                 :      46365 :     s->bpos += n;
     353                 :      46365 :     return x;
     354                 :            : }
     355                 :            : 
     356                 :            : // -- syscall utilities --
     357                 :            : 
     358                 :         16 : JL_DLLEXPORT int jl_errno(void) JL_NOTSAFEPOINT { return errno; }
     359                 :          3 : JL_DLLEXPORT void jl_set_errno(int e) JL_NOTSAFEPOINT { errno = e; }
     360                 :            : 
     361                 :            : // -- get the number of CPU threads (logical cores) --
     362                 :            : 
     363                 :            : #ifdef _OS_WINDOWS_
     364                 :            : typedef DWORD (WINAPI *GAPC)(WORD);
     365                 :            : #ifndef ALL_PROCESSOR_GROUPS
     366                 :            : #define ALL_PROCESSOR_GROUPS 0xffff
     367                 :            : #endif
     368                 :            : #endif
     369                 :            : 
     370                 :            : // Apple's M1 processor is a big.LITTLE style processor, with 4x "performance"
     371                 :            : // cores, and 4x "efficiency" cores.  Because Julia expects to be able to run
     372                 :            : // things like heavy linear algebra workloads on all cores, it's best for us
     373                 :            : // to only spawn as many threads as there are performance cores.  Once macOS
     374                 :            : // 12 is released, we'll be able to query the multiple "perf levels" of the
     375                 :            : // cores of a CPU (see this PR [0] to pytorch/cpuinfo for an example) but
     376                 :            : // until it's released, we will just recognize the M1 by its CPU family
     377                 :            : // identifier, then subtract how many efficiency cores we know it has.
     378                 :            : 
     379                 :        563 : JL_DLLEXPORT int jl_cpu_threads(void) JL_NOTSAFEPOINT
     380                 :            : {
     381                 :            : #if defined(HW_AVAILCPU) && defined(HW_NCPU)
     382                 :            :     size_t len = 4;
     383                 :            :     int32_t count;
     384                 :            :     int nm[2] = {CTL_HW, HW_AVAILCPU};
     385                 :            :     sysctl(nm, 2, &count, &len, NULL, 0);
     386                 :            :     if (count < 1) {
     387                 :            :         nm[1] = HW_NCPU;
     388                 :            :         sysctl(nm, 2, &count, &len, NULL, 0);
     389                 :            :         if (count < 1) { count = 1; }
     390                 :            :     }
     391                 :            : 
     392                 :            : #if defined(__APPLE__) && defined(_CPU_AARCH64_)
     393                 :            : //MacOS 12 added a way to query performance cores
     394                 :            :     char buf[7];
     395                 :            :     len = 7;
     396                 :            :     sysctlbyname("kern.osrelease", buf, &len, NULL, 0);
     397                 :            :     if (buf[0] > 1 && buf[1] > 0){
     398                 :            :         len = 4;
     399                 :            :         sysctlbyname("hw.perflevel0.physicalcpu", &count, &len, NULL, 0);
     400                 :            :     }
     401                 :            :     else {
     402                 :            :         int32_t family = 0;
     403                 :            :         len = 4;
     404                 :            :         sysctlbyname("hw.cpufamily", &family, &len, NULL, 0);
     405                 :            :         if (family >= 1 && count > 1) {
     406                 :            :             if (family == CPUFAMILY_ARM_FIRESTORM_ICESTORM) {
     407                 :            :                 // We know the Apple M1 has 4 efficiency cores, so subtract them out.
     408                 :            :                 count -= 4;
     409                 :            :             }
     410                 :            :         }
     411                 :            :     }
     412                 :            : #endif
     413                 :            :     return count;
     414                 :            : #elif defined(_SC_NPROCESSORS_ONLN)
     415                 :        563 :     long count = sysconf(_SC_NPROCESSORS_ONLN);
     416         [ -  + ]:        563 :     if (count < 1)
     417                 :          0 :         return 1;
     418                 :        563 :     return count;
     419                 :            : #elif defined(_OS_WINDOWS_)
     420                 :            :     //Try to get WIN7 API method
     421                 :            :     GAPC gapc;
     422                 :            :     if (jl_dlsym(jl_kernel32_handle, "GetActiveProcessorCount", (void **)&gapc, 0)) {
     423                 :            :         return gapc(ALL_PROCESSOR_GROUPS);
     424                 :            :     }
     425                 :            :     else { //fall back on GetSystemInfo
     426                 :            :         SYSTEM_INFO info;
     427                 :            :         GetSystemInfo(&info);
     428                 :            :         return info.dwNumberOfProcessors;
     429                 :            :     }
     430                 :            : #else
     431                 :            : #warning "cpu core detection not defined for this platform"
     432                 :            :     return 1;
     433                 :            : #endif
     434                 :            : }
     435                 :            : 
     436                 :          4 : JL_DLLEXPORT int jl_effective_threads(void) JL_NOTSAFEPOINT
     437                 :            : {
     438                 :          4 :     int cpu = jl_cpu_threads();
     439                 :          4 :     int masksize = uv_cpumask_size();
     440   [ +  -  -  + ]:          4 :     if (masksize < 0 || jl_running_under_rr(0))
     441                 :          0 :         return cpu;
     442                 :          4 :     uv_thread_t tid = uv_thread_self();
     443                 :          4 :     char *cpumask = (char *)calloc(masksize, sizeof(char));
     444                 :          4 :     int err = uv_thread_getaffinity(&tid, cpumask, masksize);
     445         [ -  + ]:          4 :     if (err) {
     446                 :          0 :         free(cpumask);
     447                 :          0 :         jl_safe_printf("WARNING: failed to get thread affinity (%s %d)\n", uv_err_name(err),
     448                 :            :                        err);
     449                 :          0 :         return cpu;
     450                 :            :     }
     451                 :          4 :     int n = 0;
     452         [ +  + ]:       4100 :     for (size_t i = 0; i < masksize; i++) {
     453                 :       4096 :         n += cpumask[i];
     454                 :            :     }
     455                 :          4 :     free(cpumask);
     456                 :          4 :     return n < cpu ? n : cpu;
     457                 :            : }
     458                 :            : 
     459                 :            : 
     460                 :            : // -- high resolution timers --
     461                 :            : // Returns time in nanosec
     462                 :    4631520 : JL_DLLEXPORT uint64_t jl_hrtime(void) JL_NOTSAFEPOINT
     463                 :            : {
     464                 :    4631520 :     return uv_hrtime();
     465                 :            : }
     466                 :            : 
     467                 :            : // -- iterating the environment --
     468                 :            : 
     469                 :            : #ifdef __APPLE__
     470                 :            : #include <crt_externs.h>
     471                 :            : #else
     472                 :            : #if !defined(_OS_WINDOWS_) || defined(_COMPILER_GCC_)
     473                 :            : extern char **environ;
     474                 :            : #endif
     475                 :            : #endif
     476                 :            : 
     477                 :       8979 : JL_DLLEXPORT jl_value_t *jl_environ(int i)
     478                 :            : {
     479                 :            : #ifdef __APPLE__
     480                 :            :     char **environ = *_NSGetEnviron();
     481                 :            : #endif
     482                 :       8979 :     char *env = environ[i];
     483         [ +  + ]:       8979 :     return env ? jl_pchar_to_string(env, strlen(env)) : jl_nothing;
     484                 :            : }
     485                 :            : 
     486                 :            : // -- child process status --
     487                 :            : 
     488                 :            : #if defined _OS_WINDOWS_
     489                 :            : /* Native Woe32 API.  */
     490                 :            : #include <process.h>
     491                 :            : #define waitpid(pid,statusp,options) _cwait (statusp, pid, WAIT_CHILD)
     492                 :            : #define WAIT_T int
     493                 :            : #define WTERMSIG(x) ((x) & 0xff) /* or: SIGABRT ?? */
     494                 :            : #define WCOREDUMP(x) 0
     495                 :            : #define WEXITSTATUS(x) (((x) >> 8) & 0xff) /* or: (x) ?? */
     496                 :            : #define WIFSIGNALED(x) (WTERMSIG (x) != 0) /* or: ((x) == 3) ?? */
     497                 :            : #define WIFEXITED(x) (WTERMSIG (x) == 0) /* or: ((x) != 3) ?? */
     498                 :            : #define WIFSTOPPED(x) 0
     499                 :            : #define WSTOPSIG(x) 0 //Is this correct?
     500                 :            : #endif
     501                 :            : 
     502                 :          0 : int jl_process_exited(int status)      { return WIFEXITED(status); }
     503                 :          0 : int jl_process_signaled(int status)    { return WIFSIGNALED(status); }
     504                 :          0 : int jl_process_stopped(int status)     { return WIFSTOPPED(status); }
     505                 :            : 
     506                 :          0 : int jl_process_exit_status(int status) { return WEXITSTATUS(status); }
     507                 :          0 : int jl_process_term_signal(int status) { return WTERMSIG(status); }
     508                 :          0 : int jl_process_stop_signal(int status) { return WSTOPSIG(status); }
     509                 :            : 
     510                 :            : // -- access to std filehandles --
     511                 :            : 
     512                 :            : JL_STREAM *JL_STDIN  = (JL_STREAM*)STDIN_FILENO;
     513                 :            : JL_STREAM *JL_STDOUT = (JL_STREAM*)STDOUT_FILENO;
     514                 :            : JL_STREAM *JL_STDERR = (JL_STREAM*)STDERR_FILENO;
     515                 :            : 
     516                 :        564 : JL_DLLEXPORT JL_STREAM *jl_stdin_stream(void)  { return JL_STDIN; }
     517                 :        564 : JL_DLLEXPORT JL_STREAM *jl_stdout_stream(void) { return JL_STDOUT; }
     518                 :        564 : JL_DLLEXPORT JL_STREAM *jl_stderr_stream(void) { return JL_STDERR; }
     519                 :            : 
     520                 :          1 : JL_DLLEXPORT int jl_termios_size(void) {
     521                 :            : #if defined(_OS_WINDOWS_)
     522                 :            :     return 0;
     523                 :            : #else
     524                 :          1 :     return sizeof(struct termios);
     525                 :            : #endif
     526                 :            : }
     527                 :            : 
     528                 :            : // -- processor native alignment information --
     529                 :            : 
     530                 :          0 : JL_DLLEXPORT void jl_native_alignment(uint_t *int8align, uint_t *int16align, uint_t *int32align,
     531                 :            :                                       uint_t *int64align, uint_t *float32align, uint_t *float64align)
     532                 :            : {
     533                 :          0 :     *int8align = __alignof(uint8_t);
     534                 :          0 :     *int16align = __alignof(uint16_t);
     535                 :          0 :     *int32align = __alignof(uint32_t);
     536                 :          0 :     *int64align = __alignof(uint64_t);
     537                 :          0 :     *float32align = __alignof(float);
     538                 :          0 :     *float64align = __alignof(double);
     539                 :          0 : }
     540                 :            : 
     541                 :          1 : JL_DLLEXPORT jl_value_t *jl_is_char_signed(void)
     542                 :            : {
     543                 :          1 :     return ((char)255) < 0 ? jl_true : jl_false;
     544                 :            : }
     545                 :            : 
     546                 :            : // -- misc sysconf info --
     547                 :            : 
     548                 :            : #ifdef _OS_WINDOWS_
     549                 :            : static long cachedPagesize = 0;
     550                 :            : JL_DLLEXPORT long jl_getpagesize(void)
     551                 :            : {
     552                 :            :     if (!cachedPagesize) {
     553                 :            :         SYSTEM_INFO systemInfo;
     554                 :            :         GetSystemInfo (&systemInfo);
     555                 :            :         cachedPagesize = systemInfo.dwPageSize;
     556                 :            :     }
     557                 :            :     return cachedPagesize;
     558                 :            : }
     559                 :            : #else
     560                 :       1716 : JL_DLLEXPORT long jl_getpagesize(void)
     561                 :            : {
     562                 :       1716 :     long page_size = sysconf(_SC_PAGESIZE);
     563         [ -  + ]:       1716 :     assert(page_size != -1);
     564                 :       1716 :     return page_size;
     565                 :            : }
     566                 :            : #endif
     567                 :            : 
     568                 :            : #ifdef _OS_WINDOWS_
     569                 :            : static long cachedAllocationGranularity = 0;
     570                 :            : JL_DLLEXPORT long jl_getallocationgranularity(void) JL_NOTSAFEPOINT
     571                 :            : {
     572                 :            :     if (!cachedAllocationGranularity) {
     573                 :            :         SYSTEM_INFO systemInfo;
     574                 :            :         GetSystemInfo (&systemInfo);
     575                 :            :         cachedAllocationGranularity = systemInfo.dwAllocationGranularity;
     576                 :            :     }
     577                 :            :     return cachedAllocationGranularity;
     578                 :            : }
     579                 :            : #else
     580                 :          0 : JL_DLLEXPORT long jl_getallocationgranularity(void) JL_NOTSAFEPOINT
     581                 :            : {
     582                 :          0 :     return jl_getpagesize();
     583                 :            : }
     584                 :            : #endif
     585                 :            : 
     586                 :        563 : JL_DLLEXPORT long jl_SC_CLK_TCK(void)
     587                 :            : {
     588                 :            : #ifndef _OS_WINDOWS_
     589                 :        563 :     return sysconf(_SC_CLK_TCK);
     590                 :            : #else
     591                 :            :     return 0;
     592                 :            : #endif
     593                 :            : }
     594                 :            : 
     595                 :            : // Takes a handle (as returned from dlopen()) and returns the absolute path to the image loaded
     596                 :       4590 : JL_DLLEXPORT const char *jl_pathname_for_handle(void *handle)
     597                 :            : {
     598         [ -  + ]:       4590 :     if (!handle)
     599                 :          0 :         return NULL;
     600                 :            : 
     601                 :            : #ifdef __APPLE__
     602                 :            :     // Iterate through all images currently in memory
     603                 :            :     for (int32_t i = _dyld_image_count() - 1; i >= 0 ; i--) {
     604                 :            :         // dlopen() each image, check handle
     605                 :            :         const char *image_name = _dyld_get_image_name(i);
     606                 :            :         void *probe_lib = jl_load_dynamic_library(image_name, JL_RTLD_DEFAULT | JL_RTLD_NOLOAD, 0);
     607                 :            :         jl_dlclose(probe_lib);
     608                 :            : 
     609                 :            :         // If the handle is the same as what was passed in (modulo mode bits), return this image name
     610                 :            :         if (((intptr_t)handle & (-4)) == ((intptr_t)probe_lib & (-4)))
     611                 :            :             return image_name;
     612                 :            :     }
     613                 :            : 
     614                 :            : #elif defined(_OS_WINDOWS_)
     615                 :            : 
     616                 :            :     wchar_t *pth16 = (wchar_t*)malloc_s(32768 * sizeof(*pth16)); // max long path length
     617                 :            :     DWORD n16 = GetModuleFileNameW((HMODULE)handle, pth16, 32768);
     618                 :            :     if (n16 <= 0) {
     619                 :            :         free(pth16);
     620                 :            :         return NULL;
     621                 :            :     }
     622                 :            :     pth16[n16] = L'\0';
     623                 :            :     DWORD n8 = WideCharToMultiByte(CP_UTF8, 0, pth16, -1, NULL, 0, NULL, NULL);
     624                 :            :     if (n8 == 0) {
     625                 :            :         free(pth16);
     626                 :            :         return NULL;
     627                 :            :     }
     628                 :            :     char *filepath = (char*)malloc_s(++n8);
     629                 :            :     if (!WideCharToMultiByte(CP_UTF8, 0, pth16, -1, filepath, n8, NULL, NULL)) {
     630                 :            :         free(pth16);
     631                 :            :         free(filepath);
     632                 :            :         return NULL;
     633                 :            :     }
     634                 :            :     free(pth16);
     635                 :            :     return filepath;
     636                 :            : 
     637                 :            : #else // Linux, FreeBSD, ...
     638                 :            : 
     639                 :            :     struct link_map *map;
     640                 :       4590 :     dlinfo(handle, RTLD_DI_LINKMAP, &map);
     641                 :            : #ifdef _COMPILER_MSAN_ENABLED_
     642                 :            :     __msan_unpoison(&map,sizeof(struct link_map*));
     643                 :            :     if (map) {
     644                 :            :         __msan_unpoison(map, sizeof(struct link_map));
     645                 :            :         __msan_unpoison_string(map->l_name);
     646                 :            :     }
     647                 :            : #endif
     648         [ +  - ]:       4590 :     if (map)
     649                 :       4590 :         return map->l_name;
     650                 :            : 
     651                 :            : #endif
     652                 :          0 :     return NULL;
     653                 :            : }
     654                 :            : 
     655                 :            : #ifdef _OS_WINDOWS_
     656                 :            : // Get a list of all the modules in this process.
     657                 :            : JL_DLLEXPORT int jl_dllist(jl_array_t *list)
     658                 :            : {
     659                 :            :     DWORD cb, cbNeeded;
     660                 :            :     HMODULE *hMods = NULL;
     661                 :            :     unsigned int i;
     662                 :            :     cbNeeded = 1024 * sizeof(*hMods);
     663                 :            :     do {
     664                 :            :         cb = cbNeeded;
     665                 :            :         hMods = (HMODULE*)realloc_s(hMods, cb);
     666                 :            :         if (!EnumProcessModulesEx(GetCurrentProcess(), hMods, cb, &cbNeeded, LIST_MODULES_ALL)) {
     667                 :            :           free(hMods);
     668                 :            :           return FALSE;
     669                 :            :         }
     670                 :            :     } while (cb < cbNeeded);
     671                 :            :     for (i = 0; i < cbNeeded / sizeof(HMODULE); i++) {
     672                 :            :         const char *path = jl_pathname_for_handle(hMods[i]);
     673                 :            :         if (path == NULL)
     674                 :            :             continue;
     675                 :            :         jl_array_grow_end((jl_array_t*)list, 1);
     676                 :            :         jl_value_t *v = jl_cstr_to_string(path);
     677                 :            :         free((char*)path);
     678                 :            :         jl_array_ptr_set(list, jl_array_dim0(list) - 1, v);
     679                 :            :     }
     680                 :            :     free(hMods);
     681                 :            :     return TRUE;
     682                 :            : }
     683                 :            : #endif
     684                 :            : 
     685                 :         11 : JL_DLLEXPORT void jl_raise_debugger(void)
     686                 :            : {
     687                 :            : #if defined(_OS_WINDOWS_)
     688                 :            :     if (IsDebuggerPresent() == 1)
     689                 :            :         DebugBreak();
     690                 :            : #else
     691                 :         11 :     raise(SIGTRAP);
     692                 :            : #endif // _OS_WINDOWS_
     693                 :         11 : }
     694                 :            : 
     695                 :          5 : JL_DLLEXPORT jl_sym_t *jl_get_UNAME(void) JL_NOTSAFEPOINT
     696                 :            : {
     697                 :          5 :     return jl_symbol(JL_BUILD_UNAME);
     698                 :            : }
     699                 :            : 
     700                 :          1 : JL_DLLEXPORT jl_sym_t *jl_get_ARCH(void) JL_NOTSAFEPOINT
     701                 :            : {
     702                 :          1 :     return jl_symbol(JL_BUILD_ARCH);
     703                 :            : }
     704                 :            : 
     705                 :        878 : JL_DLLEXPORT size_t jl_maxrss(void)
     706                 :            : {
     707                 :            : #if defined(_OS_WINDOWS_)
     708                 :            :     PROCESS_MEMORY_COUNTERS counter;
     709                 :            :     GetProcessMemoryInfo( GetCurrentProcess( ), &counter, sizeof(counter) );
     710                 :            :     return (size_t)counter.PeakWorkingSetSize;
     711                 :            : 
     712                 :            : // FIXME: `rusage` is available on OpenBSD, DragonFlyBSD and NetBSD as well.
     713                 :            : //        All of them return `ru_maxrss` in kilobytes.
     714                 :            : #elif defined(_OS_LINUX_) || defined(_OS_DARWIN_) || defined (_OS_FREEBSD_)
     715                 :            :     struct rusage rusage;
     716                 :        878 :     getrusage( RUSAGE_SELF, &rusage );
     717                 :            : 
     718                 :            : #if defined(_OS_LINUX_) || defined(_OS_FREEBSD_)
     719                 :        878 :     return (size_t)(rusage.ru_maxrss * 1024);
     720                 :            : #else
     721                 :            :     return (size_t)rusage.ru_maxrss;
     722                 :            : #endif
     723                 :            : 
     724                 :            : #else
     725                 :            :     return (size_t)0;
     726                 :            : #endif
     727                 :            : }
     728                 :            : 
     729                 :            : // Simple `rand()` like function, with global seed and added thread-safety
     730                 :            : // (but slow and insecure)
     731                 :            : static _Atomic(uint64_t) g_rngseed;
     732                 :      17758 : JL_DLLEXPORT uint64_t jl_rand(void) JL_NOTSAFEPOINT
     733                 :            : {
     734                 :      17758 :     uint64_t max = UINT64_MAX;
     735                 :      17758 :     uint64_t unbias = UINT64_MAX;
     736                 :      17758 :     uint64_t rngseed0 = jl_atomic_load_relaxed(&g_rngseed);
     737                 :            :     uint64_t rngseed;
     738                 :            :     uint64_t rnd;
     739                 :            :     do {
     740                 :      17758 :         rngseed = rngseed0;
     741                 :      17758 :         rnd = cong(max, unbias, &rngseed);
     742         [ -  + ]:      17758 :     } while (!jl_atomic_cmpswap_relaxed(&g_rngseed, &rngseed0, rngseed));
     743                 :      17758 :     return rnd;
     744                 :            : }
     745                 :            : 
     746                 :        575 : JL_DLLEXPORT void jl_srand(uint64_t rngseed) JL_NOTSAFEPOINT
     747                 :            : {
     748                 :        575 :     jl_atomic_store_relaxed(&g_rngseed, rngseed);
     749                 :        575 : }
     750                 :            : 
     751                 :        573 : void jl_init_rand(void) JL_NOTSAFEPOINT
     752                 :            : {
     753                 :            :     uint64_t rngseed;
     754         [ -  + ]:        573 :     if (uv_random(NULL, NULL, &rngseed, sizeof(rngseed), 0, NULL)) {
     755                 :          0 :         ios_puts("WARNING: Entropy pool not available to seed RNG; using ad-hoc entropy sources.\n", ios_stderr);
     756                 :          0 :         rngseed = uv_hrtime();
     757                 :          0 :         rngseed ^= int64hash(uv_os_getpid());
     758                 :            :     }
     759                 :        573 :     jl_srand(rngseed);
     760                 :        573 :     srand(rngseed);
     761                 :        573 : }
     762                 :            : 
     763                 :            : #ifdef __cplusplus
     764                 :            : }
     765                 :            : #endif

Generated by: LCOV version 1.14