diff options
Diffstat (limited to 'lib/os_mon')
-rw-r--r-- | lib/os_mon/c_src/cpu_sup.c | 96 | ||||
-rw-r--r-- | lib/os_mon/src/cpu_sup.erl | 13 | ||||
-rw-r--r-- | lib/os_mon/src/disksup.erl | 2 | ||||
-rw-r--r-- | lib/os_mon/src/memsup.erl | 56 | ||||
-rw-r--r-- | lib/os_mon/test/cpu_sup_SUITE.erl | 2 |
5 files changed, 122 insertions, 47 deletions
diff --git a/lib/os_mon/c_src/cpu_sup.c b/lib/os_mon/c_src/cpu_sup.c index 61bf1b0352..353c7e674e 100644 --- a/lib/os_mon/c_src/cpu_sup.c +++ b/lib/os_mon/c_src/cpu_sup.c @@ -47,6 +47,10 @@ #include <kstat.h> #endif +#if (defined(__APPLE__) && defined(__MACH__)) +#include <mach/mach.h> +#endif + #include <errno.h> #if defined(__sun__) || defined(__linux__) @@ -73,6 +77,10 @@ typedef struct { #endif +#if (defined(__APPLE__) && defined(__MACH__)) +#define CU_OSX_VALUES (5) +#endif + #if defined(__FreeBSD__) #include <sys/resource.h> #include <sys/sysctl.h> @@ -164,7 +172,7 @@ static int processors_online() { } #endif -#if defined(__FreeBSD__) +#if (defined(__APPLE__) && defined(__MACH__)) || defined(__FreeBSD__) void getsysctl(const char *, void *, size_t); #endif @@ -173,7 +181,7 @@ int main(int argc, char** argv) { int rc; int sz; unsigned int *rv; -#if defined(__linux__) || defined(__FreeBSD__) +#if defined(__linux__) || (defined(__APPLE__) && defined(__MACH__)) ||defined(__FreeBSD__) unsigned int no_of_cpus = 0; #endif @@ -190,6 +198,13 @@ int main(int argc, char** argv) { } #endif +#if (defined(__APPLE__) && defined(__MACH__)) + getsysctl("hw.ncpu", &no_of_cpus, sizeof(int)); + if ( (rv = (unsigned int*)malloc(sizeof(unsigned int)*(2 + 2*no_of_cpus*CU_OSX_VALUES))) == NULL) { + error("cpu_sup: malloc error"); + } +#endif + #if defined(__FreeBSD__) getsysctl("hw.ncpu", &no_of_cpus, sizeof(int)); if ( (rv = (unsigned int*)malloc(sizeof(unsigned int)*(2 + 2*no_of_cpus*CU_BSD_VALUES))) == NULL) { @@ -222,7 +237,7 @@ int main(int argc, char** argv) { case AVG5: bsd_loadavg(1); break; case AVG15: bsd_loadavg(2); break; #endif -#if defined(__sun__) || defined(__linux__) || defined(__FreeBSD__) +#if defined(__sun__) || defined(__linux__) || (defined(__APPLE__) && defined(__MACH__)) || defined(__FreeBSD__) case UTIL: util_measure(&rv,&sz); sendv(rv, sz); break; #endif case QUIT: free((void*)rv); return 0; @@ -538,20 +553,60 @@ static void util_measure(unsigned int **result_vec, int *result_sz) { #endif /* ---------------------------- * - * FreeBSD stat functions * + * OSX util functions * * ---------------------------- */ -#if defined(__FreeBSD__) +#if (defined(__APPLE__) && defined(__MACH__)) -#define EXIT_WITH(msg) (rich_error(msg, __FILE__, __LINE__)) -#define RICH_BUFLEN (213) /* left in error(char*) */ +static void util_measure(unsigned int **result_vec, int *result_sz) { + natural_t no_of_cpus; + processor_info_array_t info_array; + mach_msg_type_number_t info_count; + mach_port_t host_port; + kern_return_t error; + processor_cpu_load_info_data_t *cpu_load_info = NULL; + unsigned int *rv = NULL; + int i; -void rich_error(const char *reason, const char *file, const int line) { - char buf[RICH_BUFLEN]; - snprintf(buf, RICH_BUFLEN, "%s (%s:%i)", reason, file, line); - error(buf); + host_port = mach_host_self(); + error = host_processor_info(host_port, PROCESSOR_CPU_LOAD_INFO, + &no_of_cpus, &info_array, &info_count); + if (error != KERN_SUCCESS) { + *result_sz = 0; + return; + } + mach_port_deallocate(mach_task_self(), host_port); + cpu_load_info = (processor_cpu_load_info_data_t *) info_array; + + rv = *result_vec; + rv[0] = no_of_cpus; + rv[1] = CU_OSX_VALUES; + ++rv; /* first value is number of cpus */ + ++rv; /* second value is number of entries */ + + for (i = 0; i < no_of_cpus; ++i) { + rv[0] = CU_CPU_ID; rv[1] = i; + rv[2] = CU_USER; rv[3] = cpu_load_info[i].cpu_ticks[CPU_STATE_USER]; + rv[4] = CU_NICE_USER; rv[5] = cpu_load_info[i].cpu_ticks[CPU_STATE_NICE]; + rv[6] = CU_KERNEL; rv[7] = cpu_load_info[i].cpu_ticks[CPU_STATE_SYSTEM]; + rv[8] = CU_IDLE; rv[9] = cpu_load_info[i].cpu_ticks[CPU_STATE_IDLE]; + rv += CU_OSX_VALUES*2; + } + + *result_sz = 2 + 2*CU_OSX_VALUES * no_of_cpus; + + error = vm_deallocate(mach_task_self(), (vm_address_t)info_array, + info_count * sizeof(int)); + if (error != KERN_SUCCESS) + *result_sz = 0; } -#undef RICH_BUFLEN +#endif + +/* ---------------------------- * + * FreeBSD stat functions * + * ---------------------------- */ + +#if defined(__FreeBSD__) static void util_measure(unsigned int **result_vec, int *result_sz) { int no_of_cpus; @@ -588,6 +643,23 @@ static void util_measure(unsigned int **result_vec, int *result_sz) { *result_sz = 2 + 2*CU_BSD_VALUES * no_of_cpus; } +#endif + +/* ---------------------------- * + * Utils for OSX and FreeBSD * + * ---------------------------- */ + +#if (defined(__APPLE__) && defined(__MACH__)) || defined(__FreeBSD__) + +#define EXIT_WITH(msg) (rich_error(msg, __FILE__, __LINE__)) +#define RICH_BUFLEN (213) /* left in error(char*) */ + +void rich_error(const char *reason, const char *file, const int line) { + char buf[RICH_BUFLEN]; + snprintf(buf, RICH_BUFLEN, "%s (%s:%i)", reason, file, line); + error(buf); +} +#undef RICH_BUFLEN void getsysctl(const char *name, void *ptr, size_t len) { diff --git a/lib/os_mon/src/cpu_sup.erl b/lib/os_mon/src/cpu_sup.erl index 5664615230..e758b63d19 100644 --- a/lib/os_mon/src/cpu_sup.erl +++ b/lib/os_mon/src/cpu_sup.erl @@ -162,7 +162,8 @@ handle_call({?util, D, PC}, {Client, _Tag}, #state{os_type = {unix, Flavor}} = State) when Flavor == sunos; Flavor == linux; - Flavor == freebsd -> + Flavor == freebsd; + Flavor == darwin -> case measurement_server_call(State#state.server, {?util, D, PC, Client}) of {error, Reason} -> { reply, @@ -531,11 +532,11 @@ measurement_server_loop(State) -> measurement_server_loop(State) end; {Pid, Request} -> - try get_uint32_measurement(Request, State) of - Result -> Pid ! {data, Result} - catch - Error -> Pid ! {error, Error} - end, + _ = try get_uint32_measurement(Request, State) of + Result -> Pid ! {data, Result} + catch + Error -> Pid ! {error, Error} + end, measurement_server_loop(State); {'EXIT', OldPid, _n} when State#internal.port == OldPid -> {ok, NewPid} = port_server_start_link(), diff --git a/lib/os_mon/src/disksup.erl b/lib/os_mon/src/disksup.erl index 2b5447cfcb..492e4814da 100644 --- a/lib/os_mon/src/disksup.erl +++ b/lib/os_mon/src/disksup.erl @@ -153,7 +153,7 @@ handle_cast(_Msg, State) -> handle_info(timeout, State) -> NewDiskData = check_disk_space(State#state.os, State#state.port, State#state.threshold), - timer:send_after(State#state.timeout, timeout), + {ok, _Tref} = timer:send_after(State#state.timeout, timeout), {noreply, State#state{diskdata = NewDiskData}}; handle_info({'EXIT', _Port, Reason}, State) -> {stop, {port_died, Reason}, State#state{port=not_used}}; diff --git a/lib/os_mon/src/memsup.erl b/lib/os_mon/src/memsup.erl index 833e1ce6d1..d9d3083540 100644 --- a/lib/os_mon/src/memsup.erl +++ b/lib/os_mon/src/memsup.erl @@ -408,18 +408,18 @@ handle_info({collected_sys, {Alloc,Total}}, State) -> %% Last, if this was a periodic check, start a timer for the next %% one. New timeout = interval-time spent collecting, - case lists:member(reg, State#state.pending) of - true -> - Time = case State2#state.timeout - TimeSpent of - MS when MS<0 -> - 0; - MS -> - MS - end, - erlang:send_after(Time, self(), time_to_collect); - false -> - ignore - end, + _ = case lists:member(reg, State#state.pending) of + true -> + Time = case State2#state.timeout - TimeSpent of + MS when MS<0 -> + 0; + MS -> + MS + end, + erlang:send_after(Time, self(), time_to_collect); + false -> + ignore + end, {noreply, State2#state{wd_timer=undefined, pending=[]}}; handle_info({'EXIT', Pid, normal}, State) when is_pid(Pid) -> %% Temporary pid terminating when job is done @@ -448,17 +448,17 @@ handle_info(reg_collection_timeout, State) -> %% If it is a periodic check which has timed out, start a timer for %% the next one %% New timeout = interval-helper timeout - case lists:member(reg, State#state.pending) of - true -> - Time = - case State#state.timeout-State#state.helper_timeout of - MS when MS<0 -> 0; - MS -> MS - end, - erlang:send_after(Time, self(), time_to_collect); - false -> - ignore - end, + _ = case lists:member(reg, State#state.pending) of + true -> + Time = + case State#state.timeout-State#state.helper_timeout of + MS when MS<0 -> 0; + MS -> MS + end, + erlang:send_after(Time, self(), time_to_collect); + false -> + ignore + end, {noreply, State#state{wd_timer=undefined, pending=[]}}; handle_info({'EXIT', Pid, cancel}, State) when is_pid(Pid) -> %% Temporary pid terminating as ordered @@ -469,7 +469,7 @@ handle_info({collected_ext_sys, SysMemUsage}, State) -> %% Cancel watchdog timer (and as a security mearure, %% also flush any ext_collection_timeout message) - erlang:cancel_timer(State#state.ext_wd_timer), + ok = erlang:cancel_timer(State#state.ext_wd_timer, [{async,true}]), flush(ext_collection_timeout), %% Send the reply to all waiting clients, preserving time order @@ -535,7 +535,7 @@ code_change(Vsn, PrevState, "1.8") -> undefined -> ignore; TimerRef1 -> - erlang:cancel_timer(TimerRef1), + ok = erlang:cancel_timer(TimerRef1, [{async,true}]), SysOnly = PrevState#state.sys_only, MemUsage = dummy_reply(get_memory_data, SysOnly), SysMemUsage1 = dummy_reply(get_system_memory_data), @@ -545,7 +545,7 @@ code_change(Vsn, PrevState, "1.8") -> undefined -> ignore; TimerRef2 -> - erlang:cancel_timer(TimerRef2), + ok = erlang:cancel_timer(TimerRef2, [{async,true}]), SysMemUsage2 = dummy_reply(get_system_memory_data), reply(PrevState#state.pending, undef, SysMemUsage2) end, @@ -589,7 +589,7 @@ code_change(Vsn, PrevState, "1.8") -> undefined -> ignore; TimerRef1 -> - erlang:cancel_timer(TimerRef1), + ok = erlang:cancel_timer(TimerRef1, [{async,true}]), MemUsage = dummy_reply(get_memory_data, SysOnly), Pending2 = lists:map(fun(From) -> {reg,From} end, Pending), @@ -599,7 +599,7 @@ code_change(Vsn, PrevState, "1.8") -> undefined -> ignore; TimerRef2 -> - erlang:cancel_timer(TimerRef2), + ok = erlang:cancel_timer(TimerRef2, [{async,true}]), SysMemUsage = dummy_reply(get_system_memory_data), ExtPending2 = lists:map(fun(From) -> {ext,From} end, ExtPending), diff --git a/lib/os_mon/test/cpu_sup_SUITE.erl b/lib/os_mon/test/cpu_sup_SUITE.erl index 11648d3547..41115ee6e2 100644 --- a/lib/os_mon/test/cpu_sup_SUITE.erl +++ b/lib/os_mon/test/cpu_sup_SUITE.erl @@ -63,6 +63,8 @@ all() -> [load_api, util_api, util_values, port, unavailable]; {unix, freebsd} -> [load_api, util_api, util_values, port, unavailable]; + {unix, darwin} -> + [load_api, util_api, util_values, port, unavailable]; {unix, _OSname} -> [load_api]; _OS -> [unavailable] end. |