Skip to content

Instantly share code, notes, and snippets.

@tjumyk
Created November 4, 2017 05:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tjumyk/7319643db1d6b18a93343a185b7c2e8f to your computer and use it in GitHub Desktop.
Save tjumyk/7319643db1d6b18a93343a185b7c2e8f to your computer and use it in GitHub Desktop.
GPU Provider for Ubuntu indicator-multiload
/******************************************************************************
* Copyright (C) 2017 Yukai Miao <tjumyk@gmail.com> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License along *
* with this program; if not, write to the Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
******************************************************************************/
public class GpuProvider : Provider {
public GpuProvider() {
base("gpu", {"util", "memtotal", "memused", "memfree"}, 'p');
}
public override void update() {
try {
string[] spawn_args = {"nvidia-smi", "-i", "0", "--query-gpu=utilization.gpu,memory.total,memory.used,memory.free", "--format=csv,noheader,nounits"};
string spawn_stdout;
string spawn_stderr;
int spawn_status;
Process.spawn_sync(null, spawn_args, null, SpawnFlags.SEARCH_PATH, null, out spawn_stdout, out spawn_stderr, out spawn_status);
string[] columns = spawn_stdout.split(", ");
this.values[0] = double.parse(columns[0]) / 100.0;
for(int i = 1 ; i < 4; ++i)
this.values[i] = double.parse(columns[i]) * 1000000;
} catch (SpawnError e) {
stdout.printf ("Error: %s\n", e.message);
}
}
}
@yozhikoff
Copy link

Thanks for the code!
Can I ask about how should one use it?
Add this provider to the providers list and recompile the package?

@tjumyk
Copy link
Author

tjumyk commented Sep 9, 2020

Thanks for the code!
Can I ask about how should one use it?
Add this provider to the providers list and recompile the package?

Yes, add this file to src/ folder, add 'new GpuProvider()' to providers.vala, and then recompile the code.

See https://answers.launchpad.net/indicator-multiload/+question/660325

@yozhikoff
Copy link

Thanks for the link!
It works just perfect

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment