From 70be888f3b6e6496dc0a39d3b1dbdfaff70c0862 Mon Sep 17 00:00:00 2001 From: Gilbert Date: Fri, 5 Jul 2019 23:10:13 +0800 Subject: [PATCH] Compat windows (#2665) --- include/emqx.hrl | 11 +++++++++++ src/emqx_os_mon.erl | 9 +++------ src/emqx_vm.erl | 8 +++++--- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/include/emqx.hrl b/include/emqx.hrl index e206ad51d..7afe009a2 100644 --- a/include/emqx.hrl +++ b/include/emqx.hrl @@ -152,6 +152,7 @@ %%-------------------------------------------------------------------- %% Banned %%-------------------------------------------------------------------- + -type(banned_who() :: {client_id, binary()} | {username, binary()} | {ip_address, inet:ip_address()}). @@ -165,3 +166,13 @@ }). -endif. + +%%-------------------------------------------------------------------- +%% Compatible with Windows +%%-------------------------------------------------------------------- + +-define(compat_windows(Expression, Default), + case os:type() of + {win32, nt} -> Default; + _Unix -> Expression + end). diff --git a/src/emqx_os_mon.erl b/src/emqx_os_mon.erl index 21624c079..967f94ea2 100644 --- a/src/emqx_os_mon.erl +++ b/src/emqx_os_mon.erl @@ -47,10 +47,7 @@ -define(OS_MON, ?MODULE). --define(compat_windows(Expression), case os:type() of - {win32, nt} -> windows; - _Unix -> Expression - end). +-include("emqx.hrl"). %%------------------------------------------------------------------------------ %% API @@ -100,7 +97,7 @@ set_procmem_high_watermark(Float) -> %%------------------------------------------------------------------------------ init([Opts]) -> - _ = ?compat_windows(cpu_sup:util()), + _ = ?compat_windows(cpu_sup:util(), windows), set_mem_check_interval(proplists:get_value(mem_check_interval, Opts, 60)), set_sysmem_high_watermark(proplists:get_value(sysmem_high_watermark, Opts, 0.70)), set_procmem_high_watermark(proplists:get_value(procmem_high_watermark, Opts, 0.05)), @@ -135,7 +132,7 @@ handle_info({timeout, Timer, check}, State = #{timer := Timer, cpu_high_watermark := CPUHighWatermark, cpu_low_watermark := CPULowWatermark, is_cpu_alarm_set := IsCPUAlarmSet}) -> - case ?compat_windows(cpu_sup:util()) of + case ?compat_windows(cpu_sup:util(), windows) of 0 -> {noreply, State#{timer := undefined}}; {error, Reason} -> diff --git a/src/emqx_vm.erl b/src/emqx_vm.erl index fcc9b3067..46c038732 100644 --- a/src/emqx_vm.erl +++ b/src/emqx_vm.erl @@ -159,6 +159,8 @@ sndbuf, tos]). +-include("emqx.hrl"). + schedulers() -> erlang:system_info(schedulers). @@ -167,9 +169,9 @@ microsecs() -> (Mega * 1000000 + Sec) * 1000000 + Micro. loads() -> - [{load1, ftos(cpu_sup:avg1()/256)}, - {load5, ftos(cpu_sup:avg5()/256)}, - {load15, ftos(cpu_sup:avg15()/256)}]. + [{load1, ftos(?compat_windows(cpu_sup:avg1()/256, 0.0))}, + {load5, ftos(?compat_windows(cpu_sup:avg5()/256, 0.0))}, + {load15, ftos(?compat_windows(cpu_sup:avg15()/256, 0.0))}]. get_system_info() -> [{Key, format_system_info(Key, get_system_info(Key))} || Key <- ?SYSTEM_INFO].