Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

CPU Utilization (via WinAPI?)

yeah.. Im looking for a way to get the "CPU utilization" for a single process (Game.exe I think ;) )

I thought this would be possible with a WinAPI command..

Does anybody know how to do that?

Edit: I mean this value you find in the Task Manager, too (for every process there is the name, the user, cpu utilization and memory useage..)
 
I don't see any way offhand to get that out of the C API (there are a couple of C# things that look relevant, search MSDN). However, why do you want this? There may be an easier way.
 
I already checked MDSN.. there are some things which may help.. but because I dont know much about API at all, I dont know how to make these things useful.. I found no direct function to get the cpu utilization like there is one for the priority.. (which I dont need anymore Myonosken ;) )

and.. yeah.. you're right it is for the antilag script.. ^^
 
ok.. I think I found something which could be useful.. but.. I dont know how to use it ^^

GetProcessTimes

does somebody know how to impletement this in rgss? I dont get it.. the game always crashes.. :(

edit: sry for double posting ;)

edit2: another maybe useful thing: 'klick'
but I dont know how this could be useful in ruby.. -.-
 
mh.. this also didnt help me.. but I managed it myself..

Code:
def get_cpu_utilization
    
    @GetProcessTimes = Win32API.new('kernel32', 'GetProcessTimes', ['i','p','p','p','p'], 'i')

    # uses API Call to get the Kernel and User Time
    creation_time = '0' * 10
    exit_time = '0' * 10
    kernel_time = '0' * 10
    user_time = '0' * 10
    @GetProcessTimes.call(-1,creation_time, exit_time, kernel_time, user_time)
        
    # converts times into integer (in 100ns)
    kernel_time = kernel_time.unpack('l2')
    user_time = user_time.unpack('l2')
    kernel_time = kernel_time[0] + kernel_time[1]
    user_time = user_time[0] + user_time[1]
    
    # takes differences to calculate cpu utilization
    if @old_time != nil
      timer_difference = Time.new - @old_timer
      time_difference = kernel_time + user_time - @old_time
      result = time_difference / timer_difference / 100000
    else
      result = 0
    end
    
    # saves values (to calculate the differences, s.a.)
    @old_timer = Time.new
    @old_time = kernel_time + user_time
    
    return result
    
  end
(has to be called at least twice.. ;) )

works fine ;)
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top