1 // Copyright (C) 2025 Huawei Device Co., Ltd.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 use std::sync::{Arc, Mutex, Once};
15
16 struct RequestTaskCount {
17 completed_task_count: i32,
18 failed_task_count: i32,
19 load_state: bool,
20 }
21
22 impl RequestTaskCount {
get_instance() -> Arc<Mutex<RequestTaskCount>>23 fn get_instance() -> Arc<Mutex<RequestTaskCount>> {
24 static mut TASK_COUNT: Option<Arc<Mutex<RequestTaskCount>>> = None;
25 static ONCE: Once = Once::new();
26 ONCE.call_once(|| {
27 unsafe {
28 TASK_COUNT = Some(Arc::new(Mutex::new(RequestTaskCount {
29 completed_task_count: 0,
30 failed_task_count: 0,
31 load_state: false,
32 })))
33 };
34 });
35
36 unsafe { TASK_COUNT.as_ref().unwrap().clone() }
37 }
38 }
39
task_complete_add()40 pub(crate) fn task_complete_add() {
41 let instance = RequestTaskCount::get_instance();
42 let mut task_count = instance.lock().unwrap();
43 task_count.completed_task_count += 1;
44 task_count.load_state = true;
45 }
46
task_fail_add()47 pub(crate) fn task_fail_add() {
48 let instance = RequestTaskCount::get_instance();
49 let mut task_count = instance.lock().unwrap();
50 task_count.failed_task_count += 1;
51 task_count.load_state = true;
52 }
53
task_unload()54 pub(crate) fn task_unload() {
55 let instance = RequestTaskCount::get_instance();
56 let mut task_count = instance.lock().unwrap();
57 if task_count.load_state {
58 let completed = task_count.completed_task_count;
59 let failed = task_count.failed_task_count;
60 sys_event!(
61 ExecError,
62 DfxCode::TASK_STATISTICS,
63 &format!("Task Completed {}, failed {}", completed, failed)
64 );
65 task_count.completed_task_count = 0;
66 task_count.failed_task_count = 0;
67 task_count.load_state = false;
68 }
69 }
70