44//! losely to the target but instead, without coordination, merely generates
55//! a process tree.
66
7- use crate :: { signals:: Shutdown , throttle:: Throttle } ;
7+ use crate :: {
8+ signals:: Shutdown ,
9+ throttle:: { self , Throttle } ,
10+ } ;
811use is_executable:: IsExecutable ;
912use nix:: {
1013 sys:: wait:: { waitpid, WaitPidFlag , WaitStatus } ,
@@ -107,7 +110,7 @@ fn default_envs_count() -> NonZeroU32 {
107110 NonZeroU32 :: new ( 10 ) . unwrap ( )
108111}
109112
110- #[ derive( Debug , Serialize , Deserialize , PartialEq , Eq , Clone ) ]
113+ #[ derive( Debug , Serialize , Deserialize , PartialEq , Clone ) ]
111114/// Configuration of [`ProcessTree`]
112115#[ serde( tag = "mode" , rename_all = "snake_case" ) ]
113116pub enum Args {
@@ -117,14 +120,14 @@ pub enum Args {
117120 Generate ( GenerateArgs ) ,
118121}
119122
120- #[ derive( Debug , Serialize , Deserialize , PartialEq , Eq , Clone ) ]
123+ #[ derive( Debug , Serialize , Deserialize , PartialEq , Clone ) ]
121124/// Configuration of [`ProcessTree`]
122125pub struct StaticArgs {
123126 /// Argumments used with the `static` mode
124127 pub values : Vec < String > ,
125128}
126129
127- #[ derive( Debug , Serialize , Deserialize , PartialEq , Eq , Clone , Copy ) ]
130+ #[ derive( Debug , Serialize , Deserialize , PartialEq , Clone , Copy ) ]
128131/// Configuration of [`ProcessTree`]
129132pub struct GenerateArgs {
130133 /// The maximum number argument per Process. Used by the `generate` mode
@@ -135,7 +138,7 @@ pub struct GenerateArgs {
135138 pub count : NonZeroU32 ,
136139}
137140
138- #[ derive( Debug , Serialize , Deserialize , PartialEq , Eq , Clone ) ]
141+ #[ derive( Debug , Serialize , Deserialize , PartialEq , Clone ) ]
139142/// Configuration of [`ProcessTree`]
140143#[ serde( tag = "mode" , rename_all = "snake_case" ) ]
141144pub enum Envs {
@@ -145,14 +148,14 @@ pub enum Envs {
145148 Generate ( GenerateEnvs ) ,
146149}
147150
148- #[ derive( Debug , Serialize , Deserialize , PartialEq , Eq , Clone ) ]
151+ #[ derive( Debug , Serialize , Deserialize , PartialEq , Clone ) ]
149152/// Configuration of [`ProcessTree`]
150153pub struct StaticEnvs {
151154 /// Environment variables used with the `static` mode
152155 pub values : Vec < String > ,
153156}
154157
155- #[ derive( Debug , Serialize , Deserialize , PartialEq , Eq , Clone , Copy ) ]
158+ #[ derive( Debug , Serialize , Deserialize , PartialEq , Clone , Copy ) ]
156159/// Configuration of [`ProcessTree`]
157160pub struct GenerateEnvs {
158161 /// The maximum number environment variable per Process. Used by the `generate` mode
@@ -179,7 +182,7 @@ impl StaticEnvs {
179182 }
180183}
181184
182- #[ derive( Debug , Serialize , Deserialize , PartialEq , Eq , Clone ) ]
185+ #[ derive( Debug , Serialize , Deserialize , PartialEq , Clone ) ]
183186/// Configuration of [`ProcessTree`]
184187pub struct Executable {
185188 /// Path of the executable
@@ -190,7 +193,7 @@ pub struct Executable {
190193 pub envs : Envs ,
191194}
192195
193- #[ derive( Debug , Serialize , Deserialize , PartialEq , Eq , Clone ) ]
196+ #[ derive( Debug , Serialize , Deserialize , PartialEq , Clone ) ]
194197/// Configuration of [`ProcessTree`]
195198pub struct Config {
196199 /// The seed for random operations against this target
@@ -209,6 +212,9 @@ pub struct Config {
209212 pub process_sleep_ns : NonZeroU32 ,
210213 /// List of executables
211214 pub executables : Vec < Executable > ,
215+ /// The load throttle configuration
216+ #[ serde( default ) ]
217+ pub throttle : throttle:: Config ,
212218}
213219
214220impl Config {
@@ -239,7 +245,7 @@ impl Config {
239245pub struct ProcessTree {
240246 lading_path : PathBuf ,
241247 config_content : String ,
242- max_tree_per_second : NonZeroU32 ,
248+ throttle : Throttle ,
243249 shutdown : Shutdown ,
244250}
245251
@@ -256,11 +262,12 @@ impl ProcessTree {
256262 Err ( e) => return Err ( Error :: from ( e) ) ,
257263 } ;
258264
265+ let throttle = Throttle :: new_with_config ( config. throttle , config. max_tree_per_second ) ;
259266 match serde_yaml:: to_string ( config) {
260267 Ok ( serialized) => Ok ( Self {
261268 lading_path,
262269 config_content : serialized,
263- max_tree_per_second : config . max_tree_per_second ,
270+ throttle ,
264271 shutdown,
265272 } ) ,
266273 Err ( e) => Err ( Error :: from ( e) ) ,
@@ -280,12 +287,11 @@ impl ProcessTree {
280287 /// Panic if the lading path can't determine.
281288 ///
282289 pub async fn spin ( mut self ) -> Result < ( ) , Error > {
283- let mut process_throttle = Throttle :: new ( self . max_tree_per_second ) ;
284290 let lading_path = self . lading_path . to_str ( ) . unwrap ( ) ;
285291
286292 loop {
287293 tokio:: select! {
288- _ = process_throttle . wait( ) => {
294+ _ = self . throttle . wait( ) => {
289295 // using pid as target pid just to pass laging clap constraints
290296 let output = Command :: new( lading_path)
291297 . args( [ "--target-pid" , "1" ] )
0 commit comments