13
13
// see <https://www.gnu.org/licenses/>.
14
14
15
15
use anyhow:: { Result , bail} ;
16
+ use chrono:: DateTime ;
16
17
use chrono:: { TimeZone , Utc , offset:: LocalResult } ;
18
+ use perf_event_rs:: sampling:: OverflowBy ;
19
+ use psh_proto:: task:: TaskType ;
17
20
use psh_proto:: {
18
21
ExportDataReq , GetTaskReq , HeartbeatReq , TaskDoneReq , Unit ,
19
22
psh_service_client:: PshServiceClient ,
@@ -26,7 +29,7 @@ use tonic::{
26
29
transport:: { Channel , ClientTlsConfig , Endpoint } ,
27
30
} ;
28
31
29
- use crate :: { config:: RpcConfig , runtime:: Task , services:: host_info:: new_info_req} ;
32
+ use crate :: { config:: RpcConfig , runtime:: WasmTask , services:: host_info:: new_info_req} ;
30
33
31
34
#[ derive( Clone ) ]
32
35
pub struct RpcClient {
80
83
}
81
84
}
82
85
86
+ pub enum WhichTask {
87
+ Wasm ( WasmTask ) ,
88
+ Profiling ( ProfilingTask ) ,
89
+ }
90
+
91
+ pub struct ProfilingTask {
92
+ pub id : Option < String > ,
93
+ pub process : perf_event_rs:: config:: Process ,
94
+ pub mmap_pages : u64 ,
95
+ pub overflow_by : OverflowBy ,
96
+ pub stack_depth : Option < u16 > ,
97
+ pub end_time : DateTime < Utc > ,
98
+ }
99
+
83
100
impl RpcClient {
84
101
pub async fn new ( config : & RpcConfig , token : String ) -> Result < Self > {
85
102
let ep = Endpoint :: from_shared ( config. addr . clone ( ) ) ?
@@ -132,7 +149,7 @@ impl RpcClient {
132
149
Ok ( ( ) )
133
150
}
134
151
135
- pub async fn get_task ( & mut self , instance_id : String ) -> Result < Option < Task > > {
152
+ pub async fn get_task ( & mut self , instance_id : String ) -> Result < Option < WhichTask > > {
136
153
let get_task_req = GetTaskReq { instance_id } ;
137
154
let token = & self . token ;
138
155
@@ -142,20 +159,41 @@ impl RpcClient {
142
159
self . client . get_task ( req) . await
143
160
} )
144
161
. await ?;
145
- let task = match response. into_inner ( ) . task {
146
- Some ( task) => task,
147
- None => return Ok ( None ) ,
162
+ let Some ( task) : Option < psh_proto:: Task > = response. into_inner ( ) . task else {
163
+ return Ok ( None ) ;
148
164
} ;
149
165
150
- let end_time = match Utc . timestamp_millis_opt ( task. end_time as _ ) {
151
- LocalResult :: Single ( t) => t,
152
- _ => bail ! ( "Invalid task end time" ) ,
166
+ let LocalResult :: Single ( end_time) = Utc . timestamp_millis_opt ( task. end_time as _ ) else {
167
+ bail ! ( "Invalid task end time" )
153
168
} ;
154
- let task = Task {
155
- id : Some ( task. id ) ,
156
- wasm_component : task. wasm ,
157
- wasm_component_args : task. wasm_args ,
158
- end_time,
169
+ let Some ( task_type) = task. task_type else {
170
+ return Ok ( None ) ;
171
+ } ;
172
+
173
+ let task = match task_type {
174
+ TaskType :: Profiling ( profiling_task) => {
175
+ let Some ( process) = profiling_task. process else {
176
+ return Ok ( None ) ;
177
+ } ;
178
+ let Some ( overflow_by) = profiling_task. overflow_by else {
179
+ return Ok ( None ) ;
180
+ } ;
181
+ let process: perf_event_rs:: config:: Process = process. into ( ) ;
182
+ WhichTask :: Profiling ( ProfilingTask {
183
+ id : task. id . into ( ) ,
184
+ process,
185
+ mmap_pages : profiling_task. mmap_pages ,
186
+ overflow_by : overflow_by. into ( ) ,
187
+ stack_depth : profiling_task. stack_depth . map ( |v| v as _ ) ,
188
+ end_time,
189
+ } )
190
+ }
191
+ TaskType :: Wasm ( wasm_task) => WhichTask :: Wasm ( WasmTask {
192
+ id : Some ( task. id ) ,
193
+ wasm_component : wasm_task. wasm ,
194
+ wasm_component_args : wasm_task. wasm_args ,
195
+ end_time,
196
+ } ) ,
159
197
} ;
160
198
161
199
Ok ( Some ( task) )
0 commit comments