Getting TPS, Ram, CPU Usage

Discussion in 'Spigot Plugin Development' started by ProJoosh, May 27, 2015.

  1. With a plugin how would i go about getting the CPU Usage of the machine its on, Ram Usage and Server TPS? Ive seen this done on other servers i just dont know how its done.
     
  2. For ram usage use a 'Runtime' object and get the max memory, divide it by 1048576L and then get the free memory and divide it by 1048576L then take away the max memory from the free memory
     
    • Agree Agree x 1
  3. Code (Text):
    newString = newString.replaceAll("<tps>", String.valueOf(Math.round(tps * 100.0D) / 100.0D));
    newString = newString.replaceAll("<freeram>", String.valueOf(Runtime.getRuntime().freeMemory() / 1024L / 1024L));
    newString = newString.replaceAll("<maxram>", String.valueOf(Runtime.getRuntime().maxMemory() / 1024L / 1024L));
    newString = newString.replaceAll("<usedram>", String.valueOf((Runtime.getRuntime().maxMemory() - Runtime.getRuntime().freeMemory()) / 1024L / 1024L));
    newString = newString.replaceAll("<availableprocessors>", String.valueOf(Runtime.getRuntime().availableProcessors()));
     
    • Like Like x 1
    • Agree Agree x 1
    • Optimistic Optimistic x 1
  4. Offtopic, but
    Code (Java):
    if (player.wantsSpoonFeeding()) player.sendMessage("Sorry, I've run out of spoons");
    Your sig :p
     
    • Funny Funny x 6
  5. Offtopic but... the WantsSpoonFeeding() has to be named wantsToBeSpoonFed().

    Ah and btw. this has been answered a lot of times before --> http://www.giyf.com
     
  6. i never asked for the direct code on how to do it, all i wanted was an anwser like For ram usage use a 'Runtime' object and get the max memory, divide it by 1048576L and then get the free memory and divide it by 1048576L then take away the max memory from the free memory not like Bear53's
     
    • Funny Funny x 1
  7. Use the plugin TOP of maximvdw?
     
  8. I think he wants to make it in his own plugin
    That code didn't even do anything by itself he would still need to put it into a working fourm
     
  9. @Bear53 you shouldn't be using replaceAll for replacing String literals (= String which is not a regular expression)

    @ProJoosh like shown before, RAM is not going to be a big issue. For TPS, you can use the recentTps field Spigot added to the MinecraftServer class (they use it in /tps as well).

    However, CPU is a tad harder to do. With reflection, you can get some numbers from
    ManagementFactory#getOperatingSystemMXBean() (the exact methods can be found here). If that fails (which is a possibility - I couldn't get the process CPU load, only the system CPU load), you can always rely on executing native commands (using Runtime#exec(String)) and reading the output from the Process's OutputStream
     
    #9 DarkSeraphim, May 27, 2015
    Last edited: May 27, 2015
    • Like Like x 1
    • Agree Agree x 1
  10. That's not the whole code... Just a snipet
     
  11. it was a more general note :p.
     
  12. There are plugins already available which do this exact thing.
     
  13. You would use ThreadMXBean and determine the usages from there.
     
  14. ThreadMXBean did not offer any direct solutions, only CPU time. (unless I'm missing something obvious here)
     
  15. Right, if you add up the cpu times, you can calculate the usage. It's not a measure of time in terms of a clock, it's the measure of cpu time used. (From there, you can take segments, calculate the difference, and even graph it).
     
    • Informative Informative x 1