|
| 1 | +%%-------------------------------------------------------------------- |
| 2 | +%% Copyright (c) 2024 EMQ Technologies Co., Ltd. All Rights Reserved. |
| 3 | +%% |
| 4 | +%% Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +%% you may not use this file except in compliance with the License. |
| 6 | +%% You may obtain a copy of the License at |
| 7 | +%% |
| 8 | +%% http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +%% |
| 10 | +%% Unless required by applicable law or agreed to in writing, software |
| 11 | +%% distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +%% See the License for the specific language governing permissions and |
| 14 | +%% limitations under the License. |
| 15 | +%%-------------------------------------------------------------------- |
| 16 | + |
| 17 | +-module(emqx_cpu_sup_worker). |
| 18 | + |
| 19 | +-behaviour(gen_server). |
| 20 | + |
| 21 | +-include("logger.hrl"). |
| 22 | + |
| 23 | +%% gen_server APIs |
| 24 | +-export([start_link/0]). |
| 25 | + |
| 26 | +-export([ |
| 27 | + cpu_util/0, |
| 28 | + cpu_util/1 |
| 29 | +]). |
| 30 | + |
| 31 | +%% gen_server callbacks |
| 32 | +-export([ |
| 33 | + init/1, |
| 34 | + handle_continue/2, |
| 35 | + handle_call/3, |
| 36 | + handle_cast/2, |
| 37 | + terminate/2, |
| 38 | + code_change/3 |
| 39 | +]). |
| 40 | + |
| 41 | +-define(CPU_USAGE_WORKER, ?MODULE). |
| 42 | + |
| 43 | +%%-------------------------------------------------------------------- |
| 44 | +%% API |
| 45 | +%%-------------------------------------------------------------------- |
| 46 | + |
| 47 | +cpu_util() -> |
| 48 | + gen_server:call(?CPU_USAGE_WORKER, ?FUNCTION_NAME, infinity). |
| 49 | + |
| 50 | +cpu_util(Args) -> |
| 51 | + gen_server:call(?CPU_USAGE_WORKER, {?FUNCTION_NAME, Args}, infinity). |
| 52 | + |
| 53 | +%%-------------------------------------------------------------------- |
| 54 | +%% gen_server callbacks |
| 55 | +%% simply handle cpu_sup:util/0,1 called in one process |
| 56 | +%%-------------------------------------------------------------------- |
| 57 | + |
| 58 | +start_link() -> |
| 59 | + gen_server:start_link({local, ?CPU_USAGE_WORKER}, ?MODULE, [], []). |
| 60 | + |
| 61 | +init([]) -> |
| 62 | + {ok, undefined, {continue, setup}}. |
| 63 | + |
| 64 | +handle_continue(setup, undefined) -> |
| 65 | + %% start os_mon temporarily |
| 66 | + {ok, _} = application:ensure_all_started(os_mon), |
| 67 | + %% The returned value of the first call to cpu_sup:util/0 or cpu_sup:util/1 by a |
| 68 | + %% process will on most systems be the CPU utilization since system boot, |
| 69 | + %% but this is not guaranteed and the value should therefore be regarded as garbage. |
| 70 | + %% This also applies to the first call after a restart of cpu_sup. |
| 71 | + _Val = cpu_sup:util(), |
| 72 | + {noreply, #{}}. |
| 73 | + |
| 74 | +handle_call(cpu_util, _From, State) -> |
| 75 | + Val = cpu_sup:util(), |
| 76 | + {reply, Val, State}; |
| 77 | +handle_call({cpu_util, Args}, _From, State) -> |
| 78 | + Val = erlang:apply(cpu_sup, util, Args), |
| 79 | + {reply, Val, State}; |
| 80 | +handle_call(Req, _From, State) -> |
| 81 | + ?SLOG(error, #{msg => "unexpected_call", call => Req}), |
| 82 | + {reply, ignored, State}. |
| 83 | + |
| 84 | +handle_cast(Msg, State) -&g
E293
t; |
| 85 | + ?SLOG(error, #{msg => "unexpected_cast", cast => Msg}), |
| 86 | + {noreply, State}. |
| 87 | + |
| 88 | +terminate(_Reason, _State) -> |
| 89 | + ok. |
| 90 | + |
| 91 | +code_change(_OldVsn, State, _Extra) -> |
| 92 | + {ok, State}. |
0 commit comments