Ticket #2782: mythtv_jobqueue_2.patch

File mythtv_jobqueue_2.patch, 3.5 KB (added by oa@…, 17 years ago)

dynamic simultaneous jobs - includes CPU monitoring for Linux only

  • libs/libmyth/util.h

     
    6161long long getDiskSpace(const QString&,long long&,long long&);
    6262bool getUptime(time_t &uptime);
    6363bool getMemStats(int &totalMB, int &freeMB, int &totalVM, int &freeVM);
     64bool getCPUStats(int &totalTime, int &idleTime);
    6465
    6566#endif // UTIL_H_
  • libs/libmyth/util.cpp

     
    472472
    473473    return true;
    474474}
     475
     476bool getCPUStats(int &totalTime, int &idleTime)
     477{
     478#ifdef __linux__
     479        bool found = false;
     480    char line[256];
     481        FILE *stat = fopen("/proc/stat", "r");
     482        if (stat)
     483        {
     484        while ( (found = fgets(line, sizeof line, stat)) && strncmp(line, "cpu ", 4) )
     485                ;
     486
     487        fclose(stat);
     488        }
     489       
     490    if (found)
     491        {
     492            char what[32];
     493            unsigned long user_time, nice_time, system_time, idle_time, wait_time = 0;
     494            sscanf(line, "%s %lu %lu %lu %lu %lu", what, &user_time, &nice_time,
     495                &system_time, &idle_time, &wait_time);
     496
     497        unsigned long total_time = user_time + nice_time + system_time + idle_time + wait_time ;
     498            static unsigned long last_total_time = 0, last_idle_time = 0;
     499            totalTime = total_time - last_total_time;
     500            last_total_time = total_time;
     501            idleTime = idle_time - last_idle_time;
     502            last_idle_time = idle_time;
     503            return true;       
     504        }
     505        else
     506        {
     507                VERBOSE(VB_IMPORTANT, "cpuCPUAvail(): can't read /proc/stat");
     508                return false;
     509        }
     510#else
     511        VERBOSE(VB_IMPORTANT, "getCPUAvail(): Unknown platform. "
     512                        "How do I get CPU stats?");
     513        return false;
     514#endif
     515}
  • libs/libmythtv/jobqueue.cpp

     
    231231                jobsRunning++;
    232232            }
    233233
     234            int totalTime, idleTime;
    234235            message = QString("Currently Running %1 jobs.")
    235236                              .arg(jobsRunning);
    236237            if (!inTimeWindow)
     
    251252
    252253                atMax = true;
    253254            }
     255            else if (getCPUStats(totalTime, idleTime) && (idleTime * 100 / totalTime) < 25)
     256            {
     257                message += QString(" CPU near full capacity (%1% idle), "
     258                                   "will not start new jobs")
     259                                   .arg(idleTime * 100 / totalTime);
     260                VERBOSE(VB_JOBQUEUE, LOC + message);
     261                atMax = true;
     262            }
    254263            else
    255264            {
    256265                VERBOSE(VB_JOBQUEUE, LOC + message);
    257266                atMax = false;
    258267            }
    259268
    260 
    261269            for (unsigned int x = 0;
    262                  (x < jobs.size()) && (jobsRunning < maxJobs); x++)
     270                 x < jobs.size(); x++)
    263271            {
    264272                id = jobs[x].id;
    265273                chanid = jobs[x].chanid;
     
    431439                }
    432440
    433441                // never start or claim more than one job in a single run
    434                 if (startedJobAlready)
     442                if (startedJobAlready || atMax)
    435443                    continue;
    436444
    437445                if ((inTimeWindow) &&
     
    470478            }
    471479        }
    472480
    473         if (startedJobAlready)
    474             sleep(5);
    475         else
    476             sleep(sleepTime);
     481        sleep(sleepTime);
    477482    }
    478483}
    479484