| 1 |
michael |
3251 |
/* PEAK Library |
| 2 |
|
|
* |
| 3 |
|
|
* Copyright (c) 2003 |
| 4 |
|
|
* Stephane Thiell <mbuna@bugged.org>. All rights reserved. |
| 5 |
|
|
* |
| 6 |
|
|
* Redistribution and use in source and binary forms, with or without |
| 7 |
|
|
* modification, are permitted provided that the following conditions |
| 8 |
|
|
* are met: |
| 9 |
|
|
* |
| 10 |
|
|
* 1. Redistributions of source code must retain the above copyright |
| 11 |
|
|
* notice, this list of conditions and the following disclaimer. |
| 12 |
|
|
* |
| 13 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
|
|
* notice, this list of conditions and the following disclaimer in the |
| 15 |
|
|
* documentation and/or other materials provided with the distribution. |
| 16 |
|
|
* |
| 17 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 18 |
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 19 |
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 20 |
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 21 |
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 22 |
|
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 23 |
|
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 24 |
|
|
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 25 |
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 26 |
|
|
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 27 |
|
|
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 |
|
|
* |
| 29 |
|
|
*/ |
| 30 |
|
|
#define RCSID "$Id: utilities.c,v 1.1.1.1 2003/12/30 02:29:39 mbuna Exp $" |
| 31 |
|
|
|
| 32 |
|
|
#ifdef HAVE_CONFIG_H |
| 33 |
|
|
#include "config.h" |
| 34 |
|
|
#endif |
| 35 |
|
|
|
| 36 |
|
|
#include "utilities.h" |
| 37 |
|
|
|
| 38 |
|
|
#include <assert.h> |
| 39 |
|
|
#include <limits.h> |
| 40 |
|
|
#include <stdlib.h> |
| 41 |
|
|
#include <unistd.h> |
| 42 |
|
|
#include <sys/types.h> |
| 43 |
|
|
#include <sys/time.h> |
| 44 |
|
|
#include <sys/param.h> |
| 45 |
|
|
#include <sys/resource.h> |
| 46 |
|
|
#ifdef HAVE_SYS_MPCTL_H |
| 47 |
|
|
#include <sys/mpctl.h> |
| 48 |
|
|
#endif |
| 49 |
|
|
#ifdef HAVE_SYS_SYSCTL_H |
| 50 |
|
|
#include <sys/sysctl.h> |
| 51 |
|
|
#endif |
| 52 |
|
|
|
| 53 |
|
|
#include "internal.h" |
| 54 |
|
|
|
| 55 |
|
|
|
| 56 |
|
|
/* peak_get_ncpus */ |
| 57 |
|
|
|
| 58 |
|
|
#if defined(HAVE_SYSCTL) && defined(CTL_HW) && defined(HW_NCPU) |
| 59 |
|
|
|
| 60 |
|
|
/* BSD sysctl */ |
| 61 |
|
|
|
| 62 |
|
|
int |
| 63 |
|
|
peak_get_ncpus() |
| 64 |
|
|
{ |
| 65 |
|
|
int mib[2], ncpus; |
| 66 |
|
|
size_t len; |
| 67 |
|
|
|
| 68 |
|
|
mib[0] = CTL_HW; |
| 69 |
|
|
mib[1] = HW_NCPU; |
| 70 |
|
|
len = sizeof(ncpus); |
| 71 |
|
|
if (sysctl(mib, 2, &ncpus, &len, NULL, 0) == -1) |
| 72 |
|
|
PEAK_HALT; |
| 73 |
|
|
return ncpus; |
| 74 |
|
|
} |
| 75 |
|
|
|
| 76 |
|
|
#elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN) |
| 77 |
|
|
|
| 78 |
|
|
/* POSIX.1 sysconf */ |
| 79 |
|
|
|
| 80 |
|
|
int |
| 81 |
|
|
peak_get_ncpus() |
| 82 |
|
|
{ |
| 83 |
|
|
return sysconf(_SC_NPROCESSORS_ONLN); |
| 84 |
|
|
} |
| 85 |
|
|
|
| 86 |
|
|
#elif defined(HAVE_MPCTL) |
| 87 |
|
|
|
| 88 |
|
|
/* HP-UX mpctl */ |
| 89 |
|
|
|
| 90 |
|
|
int |
| 91 |
|
|
peak_get_ncpus() |
| 92 |
|
|
{ |
| 93 |
|
|
return mpctl(MPC_GETNUMSPUS, NULL, NULL); |
| 94 |
|
|
} |
| 95 |
|
|
|
| 96 |
|
|
#endif |
| 97 |
|
|
|
| 98 |
|
|
|
| 99 |
|
|
/* peak_set_fdlimit */ |
| 100 |
|
|
|
| 101 |
|
|
#undef RLIMIT_FD_MAX |
| 102 |
|
|
#if defined(RLIMIT_NOFILE) |
| 103 |
|
|
# define RLIMIT_FD_MAX RLIMIT_NOFILE |
| 104 |
|
|
#elif defined(RLIMIT_FDMAX) |
| 105 |
|
|
# define RLIMIT_FD_MAX RLIMIT_FDMAX |
| 106 |
|
|
#elif defined(RLIMIT_OFILE) |
| 107 |
|
|
# define RLIMIT_FD_MAX RLIMIT_OFILE |
| 108 |
|
|
#elif defined(RLIMIT_OPEN_MAX) |
| 109 |
|
|
# define RLIMIT_FD_MAX RLIMIT_OPEN_MAX |
| 110 |
|
|
#endif |
| 111 |
|
|
|
| 112 |
|
|
#if defined(HAVE_SETRLIMIT) && defined(HAVE_SETRLIMIT) && defined(RLIMIT_FD_MAX) |
| 113 |
|
|
#define PEAK_CAN_SETRLIMIT |
| 114 |
|
|
#else |
| 115 |
|
|
#warning PEAK cannot set resource limit (setrlimit) |
| 116 |
|
|
#endif |
| 117 |
|
|
|
| 118 |
|
|
int |
| 119 |
|
|
peak_set_fdlimit(int maxfiles) |
| 120 |
|
|
{ |
| 121 |
|
|
#ifdef PEAK_CAN_SETRLIMIT |
| 122 |
|
|
struct rlimit limit; |
| 123 |
|
|
|
| 124 |
|
|
if (!getrlimit(RLIMIT_FD_MAX, &limit)) |
| 125 |
|
|
{ |
| 126 |
|
|
if (limit.rlim_max < maxfiles) |
| 127 |
|
|
return limit.rlim_max; |
| 128 |
|
|
limit.rlim_cur = limit.rlim_max; /* make soft limit the max */ |
| 129 |
|
|
setrlimit(RLIMIT_FD_MAX, &limit); |
| 130 |
|
|
} |
| 131 |
|
|
#endif |
| 132 |
|
|
return maxfiles; |
| 133 |
|
|
} |