@@ -138,6 +138,22 @@ int ThreadExecutor::handleRead(int rpipe, unsigned int &result)
138138 return 1 ;
139139}
140140
141+ bool ThreadExecutor::checkLoadAverage (size_t nchilds)
142+ {
143+ if (!nchilds || !_settings._loadAverage ) {
144+ return true ;
145+ }
146+
147+ double sample;
148+ if (getloadavg (&sample, 1 ) != 1 ) {
149+ // disable load average cheking on getloadavg error
150+ return true ;
151+ } else if (sample < _settings._loadAverage ) {
152+ return true ;
153+ }
154+ return false ;
155+ }
156+
141157unsigned int ThreadExecutor::check ()
142158{
143159 _fileCount = 0 ;
@@ -155,7 +171,8 @@ unsigned int ThreadExecutor::check()
155171 std::map<std::string, std::size_t >::const_iterator i = _files.begin ();
156172 for (;;) {
157173 // Start a new child
158- if (i != _files.end () && rpipes.size () < _settings._jobs ) {
174+ size_t nchilds = rpipes.size ();
175+ if (i != _files.end () && nchilds < _settings._jobs && checkLoadAverage (nchilds)) {
159176 int pipes[2 ];
160177 if (pipe (pipes) == -1 ) {
161178 std::cerr << " pipe() failed: " << std::strerror (errno) << std::endl;
@@ -211,8 +228,10 @@ unsigned int ThreadExecutor::check()
211228 FD_ZERO (&rfds);
212229 for (std::list<int >::const_iterator rp = rpipes.begin (); rp != rpipes.end (); ++rp)
213230 FD_SET (*rp, &rfds);
214-
215- int r = select (*std::max_element (rpipes.begin (), rpipes.end ()) + 1 , &rfds, NULL , NULL , NULL );
231+ struct timeval tv; // for every second polling of load average condition
232+ tv.tv_sec = 1 ;
233+ tv.tv_usec = 0 ;
234+ int r = select (*std::max_element (rpipes.begin (), rpipes.end ()) + 1 , &rfds, NULL , NULL , &tv);
216235
217236 if (r > 0 ) {
218237 std::list<int >::iterator rp = rpipes.begin ();
@@ -320,6 +339,11 @@ void ThreadExecutor::addFileContent(const std::string &path, const std::string &
320339 _fileContents[path] = content;
321340}
322341
342+ bool ThreadExecutor::checkLoadAverage (size_t nchilds)
343+ {
344+ return true ;
345+ }
346+
323347unsigned int ThreadExecutor::check ()
324348{
325349 HANDLE *threadHandles = new HANDLE[_settings._jobs ];
0 commit comments