Results 1 to 4 of 4

Thread: Any ideas on determining cpu load in run-time?

  1. #1
    Join Date
    Feb 2006
    Posts
    22
    Thanks
    4
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Any ideas on determining cpu load in run-time?

    Hello!
    I have some interest in determining cpu load in run-time, but have no
    idea how to implement this.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Any ideas on determining cpu load in run-time?

    man getloadavg

  3. #3
    Join Date
    Jan 2006
    Location
    Russia
    Posts
    50
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Any ideas on determining cpu load in run-time?

    Qt Code:
    1. FILE *file;
    2. unsigned proc_count = 0;
    3. long cpu = 0, nice = 0, system = 0, idle = 0, used = 0, total = 0;
    4. char buff[MAXBUFF];
    5. char *lpbuff = buff;
    6. size_t n = 0;
    7. char s_cpu_usage[MAXBUFF];
    8.  
    9. bzero(s_cpu_usage, MAXBUFF);
    10. strcpy(s_cpu_usage, "");
    11.  
    12. proc_count = get_nprocs();
    13.  
    14. file = fopen("/proc/stat", "r");
    15. if(!file)
    16. {
    17. sprintf(msg->error_str, "Error when getting CPU statistics. errno(%d) : %s", errno, strerror(errno));
    18. return 0;
    19. }
    20. //
    21. getline(&lpbuff, &n, file);
    22. for (i = 0; i < proc_count; i++)
    23. {
    24. fscanf(file, "%*s %ld %ld %ld %ld", &cpu, &nice, &system, &idle);
    25. used = cpu+nice+system;
    26. total = cpu+nice+system+idle;
    27.  
    28. bzero(buff, MAXBUFF);
    29. sprintf(lpbuff, "%ld %ld\n", used, total);
    30. strcat(s_cpu_usage, lpbuff);
    31. }
    32. fclose(file);
    To copy to clipboard, switch view to plain text mode 
    This is a part from one of my functions. I've corrected it slightly but I hope it will be clear for you.

  4. The following user says thank you to Mad Max for this useful post:

    a550ee (24th November 2006)

  5. #4
    Join Date
    Feb 2006
    Posts
    22
    Thanks
    4
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Any ideas on determining cpu load in run-time?

    Thanks all!
    It seems with some #ifdef's and your samples I can forge cross-platform solution for problem.

Similar Threads

  1. Problem with pointers while using localtime() and time()
    By jamadagni in forum General Programming
    Replies: 7
    Last Post: 11th January 2006, 15:48

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.