Skip to content

Commit d9f62ba

Browse files
committed
Initial commit
0 parents  commit d9f62ba

16 files changed

+2457
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.o
2+
*ctags
3+
build/*

CMakeLists.txt

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
cmake_minimum_required(VERSION 3.1)
2+
3+
#///////////////////////////////////////////////////////////////////#
4+
# PROJECT #
5+
#///////////////////////////////////////////////////////////////////#
6+
7+
set(NVTOP_VERSION_MAJOR 0)
8+
set(NVTOP_VERSION_MINOR 1)
9+
set(NVTOP_VERSION_PATCH 0)
10+
11+
project(nvtop
12+
LANGUAGES C)
13+
14+
# Default build type
15+
if(NOT CMAKE_BUILD_TYPE)
16+
set(CMAKE_BUILD_TYPE Release)
17+
message(STATUS "No building type advertised, default to Release")
18+
endif()
19+
20+
#///////////////////////////////////////////////////////////////////#
21+
# DEPENDENCIES #
22+
#///////////////////////////////////////////////////////////////////#
23+
24+
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules)
25+
26+
find_package(NVML REQUIRED)
27+
28+
add_library(nvml INTERFACE IMPORTED)
29+
set_property(TARGET nvml PROPERTY
30+
INTERFACE_INCLUDE_DIRECTORIES ${NVML_INCLUDE_DIRS})
31+
set_property(TARGET nvml PROPERTY
32+
INTERFACE_LINK_LIBRARIES ${NVML_LIBRARIES})
33+
34+
set(CURSES_NEED_NCURSES TRUE)
35+
find_package(Curses REQUIRED)
36+
37+
add_library(ncurses INTERFACE IMPORTED)
38+
set_property(TARGET ncurses PROPERTY
39+
INTERFACE_INCLUDE_DIRECTORIES ${CURSES_INCLUDE_DIRS})
40+
set_property(TARGET ncurses PROPERTY
41+
INTERFACE_LINK_LIBRARIES ${CURSES_LIBRARIES})
42+
43+
#///////////////////////////////////////////////////////////////////#
44+
# COMPILATION OPTIONS #
45+
#///////////////////////////////////////////////////////////////////#
46+
47+
include(cmake/compiler-flags.cmake)
48+
49+
set(CMAKE_C_FLAGS_RELEASE "-O3 -mtune=generic")
50+
set(CMAKE_C_FLAGS_DEBUG
51+
"${COMPILER_AVALIABLE_WARNINGS} ${COMPILER_ADDRESS_SANITIZER_FLAG} ${COMPILER_UNDEFINED_SANITIZER_FLAG} -Og -g")
52+
set(CMAKE_C_FLAGS_OPTIMIZED
53+
"${COMPILER_LTO_FLAG} ${COMPILER_MARCH_NATIVE} -O3")
54+
set(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELEASE} -g")
55+
56+
# Use full RPATH on build tree
57+
set(CMAKE_SKIP_BUILD_RPATH FALSE)
58+
# Do not build with install RPATH
59+
set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
60+
# Set the RPATH when install
61+
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
62+
# Only set RPATH if the installation directory is not a system directory
63+
LIST(FIND
64+
CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_PREFIX}/lib"
65+
isSystemDir)
66+
if("${isSystemDir}" STREQUAL "-1")
67+
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
68+
else()
69+
set(CMAKE_INSTALL_RPATH "")
70+
endif()
71+
72+
add_subdirectory(src)
73+
74+
#///////////////////////////////////////////////////////////////////#
75+
# INSTALL #
76+
#///////////////////////////////////////////////////////////////////#
77+
78+
install(DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/include/"
79+
DESTINATION include FILES_MATCHING PATTERN "*.h")
80+
81+
configure_file(
82+
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/cmake_uninstall.cmake.in"
83+
"${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
84+
IMMEDIATE @ONLY)
85+
add_custom_target(uninstall
86+
COMMAND ${CMAKE_COMMAND} -P
87+
${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)

COPYING

+674
Large diffs are not rendered by default.

LICENSE

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
COPYING

TODO

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
What is in the pipeline:
2+
3+
- Add command line option to configure the behaviour like
4+
- Refresh interval
5+
- Version
6+
- Whatever
7+
8+
- Instead of returning the value 0 when the NVML library errors, set an error
9+
value instead and print it accordingly in the UI.
10+
11+
- Add mouse and some keyboard shortcut for ncurses.
12+
13+
- Do a manpage

cmake/cmake_uninstall.cmake.in

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
if(NOT EXISTS "@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt")
2+
message(FATAL_ERROR
3+
"Cannot find install manifest: @CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt")
4+
endif(NOT EXISTS "@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt")
5+
6+
file(READ "@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt" files)
7+
string(REGEX REPLACE "\n" ";" files "${files}")
8+
foreach(file ${files})
9+
message(STATUS "Uninstalling $ENV{DESTDIR}${file}")
10+
if(IS_SYMLINK "$ENV{DESTDIR}${file}" OR EXISTS "$ENV{DESTDIR}${file}")
11+
exec_program( "@CMAKE_COMMAND@" ARGS "-E remove \"$ENV{DESTDIR}${file}\""
12+
OUTPUT_VARIABLE rm_out RETURN_VALUE rm_retval)
13+
if(NOT "${rm_retval}" STREQUAL 0)
14+
message(FATAL_ERROR "Problem when removing $ENV{DESTDIR}${file}")
15+
endif(NOT "${rm_retval}" STREQUAL 0)
16+
else(IS_SYMLINK "$ENV{DESTDIR}${file}" OR EXISTS "$ENV{DESTDIR}${file}")
17+
message(STATUS
18+
"File
19+
$ENV{DESTDIR}${file}
20+
does not
21+
exist.")
22+
endif(IS_SYMLINK "$ENV{DESTDIR}${file}" OR EXISTS "$ENV{DESTDIR}${file}")
23+
endforeach(file)

cmake/compiler-flags.cmake

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
include (CheckCCompilerFlag)
2+
3+
set (ALL_WARNING_FLAGS
4+
"-Waggressive-loop-optimizations"
5+
"-Wall"
6+
"-Wbad-function-cast"
7+
"-Wcast-align"
8+
"-Wcast-qual"
9+
#"-Wconversion"
10+
"-Wdisabled-optimization"
11+
"-Wdouble-promotion"
12+
"-Wextra"
13+
"-Wfloat-conversion"
14+
"-Wfloat-equal"
15+
"-Whsa"
16+
"-Winit-self"
17+
"-Wlogical-op"
18+
"-Wmissing-declarations"
19+
"-Wmissing-parameter-type"
20+
"-Wmissing-prototypes"
21+
"-Wnested-externs"
22+
"-Wnormalized=nfc"
23+
"-Wnull-dereference"
24+
"-Wold-style-declaration"
25+
"-Wold-style-definition"
26+
#"-Wpadded"
27+
"-Wpedantic"
28+
"-Wpointer-sign"
29+
"-Wshadow"
30+
"-Wstrict-aliasing"
31+
"-Wstrict-prototypes"
32+
#"-Wsuggest-attribute=const"
33+
"-Wswitch-enum"
34+
"-Wtrampolines"
35+
"-Wuninitialized"
36+
"-Wunsafe-loop-optimizations"
37+
"-Wunused-result"
38+
)
39+
40+
function (check_all_warning_flags flag_list)
41+
42+
foreach (warning_flag ${flag_list})
43+
check_c_compiler_flag(${warning_flag} "Working${warning_flag}")
44+
if ("${Working${warning_flag}}")
45+
list(APPEND VALID_WARNINGS "${warning_flag}")
46+
endif ("${Working${warning_flag}}")
47+
endforeach (warning_flag)
48+
49+
string(REPLACE ";" " " COMPILER_AVALIABLE_WARNINGS "${VALID_WARNINGS}")
50+
set (COMPILER_AVALIABLE_WARNINGS ${COMPILER_AVALIABLE_WARNINGS} PARENT_SCOPE)
51+
52+
endfunction (check_all_warning_flags)
53+
54+
check_all_warning_flags("${ALL_WARNING_FLAGS}")
55+
56+
string(REPLACE ";" " "
57+
COMPILER_AVALIABLE_WARNINGS "${COMPILER_AVALIABLE_WARNINGS}")
58+
59+
set(CMAKE_REQUIRED_FLAGS "-fsanitize=address")
60+
check_c_compiler_flag("-fsanitize=address" compiler_has_address_sanitizer)
61+
unset(CMAKE_REQUIRED_FLAGS)
62+
if(compiler_has_address_sanitizer)
63+
set(COMPILER_ADDRESS_SANITIZER_FLAG "-fsanitize=address")
64+
# Nicer stack trace
65+
check_c_compiler_flag("-fno-omit-frame-pointer"
66+
compiler_has_no_omit_frame_pointer)
67+
if(compiler_has_no_omit_frame_pointer)
68+
set(COMPILER_ADDRESS_SANITIZER_FLAG
69+
"${COMPILER_ADDRESS_SANITIZER_FLAG} -fno-omit-frame-pointer")
70+
endif()
71+
endif()
72+
73+
set(CMAKE_REQUIRED_FLAGS "-fsanitize=undefined")
74+
check_c_compiler_flag("-fsanitize=undefined" compiler_has_undefined_sanitizer)
75+
unset(CMAKE_REQUIRED_FLAGS)
76+
if(compiler_has_undefined_sanitizer)
77+
set(COMPILER_UNDEFINED_SANITIZER_FLAG "-fsanitize=undefined")
78+
endif()
79+
80+
set(CMAKE_REQUIRED_FLAGS "-flto")
81+
check_c_compiler_flag("-flto" compiler_has_lto)
82+
unset(CMAKE_REQUIRED_FLAGS)
83+
if(compiler_has_lto)
84+
set(COMPILER_LTO_FLAG "-flto")
85+
endif()
86+
87+
check_c_compiler_flag("-march=native" compiler_has_march_native)
88+
if(compiler_has_march_native)
89+
set(COMPILER_MARCH_NATIVE "-march=native")
90+
endif()

cmake/modules/FindNVML.cmake

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Version 1.1
2+
# Public Domain
3+
# Written by Maxime SCHMITT <[email protected]>
4+
5+
#/////////////////////////////////////////////////////////////////////////////#
6+
# #
7+
# Search for Nvidia nvml library on the system #
8+
# Call with find_package(NVML) #
9+
# The module defines: #
10+
# - NVML_FOUND - If NVML was found #
11+
# - NVML_INCLUDE_DIRS - the NVML include directories #
12+
# - NVML_LIBRARIES - the NVML library directories #
13+
# - NVML_API_VERSION - the NVML api version #
14+
# #
15+
#/////////////////////////////////////////////////////////////////////////////#
16+
17+
if (NVML_INCLUDE_DIRS AND NVML_LIBRARIES)
18+
set(NVML_FIND_QUIETLY TRUE)
19+
endif()
20+
21+
# Headers
22+
file(GLOB nvml_header_path_hint /usr/local/cuda*/include /opt/cuda*/include)
23+
find_path(NVML_INCLUDE_DIRS NAMES nvml.h
24+
HINTS ${nvml_header_path_hint})
25+
26+
# library
27+
find_library(NVML_LIBRARIES NAMES nvml nvidia-ml)
28+
29+
# Version
30+
set(filename "${NVML_INCLUDE_DIRS}/nvml.h")
31+
if (NOT EXISTS ${filename} AND NOT quiet)
32+
message(AUTHOR_WARNING "Unable to find ${filename}")
33+
endif()
34+
file(READ "${filename}" nvml_header)
35+
set(nvml_api_version_match "NVML_API_VERSION")
36+
37+
string(REGEX REPLACE ".*#[ \t]*define[ \t]*${nvml_api_version_match}[ \t]*([0-9]+).*"
38+
"\\1" nvml_api_version "${nvml_header}")
39+
40+
if (nvml_api_version STREQUAL nvml_header)
41+
message(AUTHOR_WARNING "Unable to find nvml api version")
42+
else()
43+
set(NVML_API_VERSION "${nvml_api_version}")
44+
endif()
45+
46+
include(FindPackageHandleStandardArgs)
47+
FIND_PACKAGE_HANDLE_STANDARD_ARGS(NVML
48+
FOUND_VAR NVML_FOUND
49+
REQUIRED_VARS NVML_INCLUDE_DIRS NVML_LIBRARIES
50+
VERSION_VAR NVML_API_VERSION)
51+
52+
mark_as_advanced(NVML_INCLUDE_DIRS NVML_LIBRARIES NVML_API_VERSION)

include/extract_gpuinfo.h

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
*
3+
* Copyright (C) 2017 Maxime Schmitt <[email protected]>
4+
*
5+
* This file is part of Nvtop.
6+
*
7+
* Nvtop is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* Nvtop is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with nvtop. If not, see <http://www.gnu.org/licenses/>.
19+
*
20+
*/
21+
22+
#ifndef __EXTRACT_GPUINFO_H_
23+
#define __EXTRACT_GPUINFO_H_
24+
25+
#include <stdbool.h>
26+
#include <nvml.h>
27+
28+
struct gpu_process {
29+
unsigned int pid; // Process ID
30+
char process_name[64]; // Process Name
31+
char user_name[64]; // Process User Name
32+
unsigned long long used_memory; // Memory used by process
33+
};
34+
35+
struct device_info {
36+
nvmlDevice_t device_handle; // Used to query device
37+
char device_name[NVML_DEVICE_NAME_BUFFER_SIZE]; // Device Name
38+
unsigned int gpu_clock_speed; // Device clock speed in MHz
39+
unsigned int gpu_clock_speed_max; // Maximum clock speed in MHz
40+
unsigned int mem_clock_speed; // Device clock speed in MHz
41+
unsigned int mem_clock_speed_max; // Maximum clock speed in MHz
42+
unsigned int gpu_util_rate; // GPU utilization rate in %
43+
unsigned int mem_util_rate; // MEM utilization rate in %
44+
unsigned int encoder_rate; // Encoder utilization rate in %
45+
unsigned int encoder_sampling; // Encoder sampling period in micro sec
46+
unsigned int decoder_rate; // Decoder utilization rate in %
47+
unsigned int decoder_sampling; // Decoder sampling period in micro sec
48+
unsigned long long free_memory; // Unallocated memory (bytes)
49+
unsigned long long total_memory; // Total memory (bytes)
50+
unsigned long long used_memory; // Allocated memory (bytes)
51+
unsigned int cur_pcie_link_gen; // PCIe link generation used
52+
unsigned int max_pcie_link_gen; // PCIe link generation max
53+
unsigned int cur_pcie_link_width; // PCIe line width used
54+
unsigned int max_pcie_link_width; // PCIe line width max
55+
unsigned int pcie_rx; // PCIe throughput in KB/s
56+
unsigned int pcie_tx; // PCIe throughput in KB/s
57+
unsigned int fan_speed; // Fan speed percentage
58+
unsigned int gpu_temp; // GPU temperature °c
59+
unsigned int gpu_temp_slowdown; // GPU temperature °c
60+
unsigned int gpu_temp_shutdown; // GPU temperature °c
61+
unsigned int power_draw; // Power usage in milliwatts
62+
unsigned int power_draw_max; // Max power usage in milliwatts
63+
unsigned int size_proc_buffers; // Number of Compute processes (Cuda)
64+
unsigned int num_compute_procs; // Number of Compute processes (Cuda)
65+
unsigned int num_graphical_procs; // Number of Graphical processes
66+
struct gpu_process *graphic_procs; // Graphical process info
67+
struct gpu_process *compute_procs; // Compute processes info
68+
nvmlProcessInfo_t *process_infos; // Internal use
69+
};
70+
71+
bool init_gpu_info_extraction(void);
72+
73+
bool shutdown_gpu_info_extraction(void);
74+
75+
unsigned int initialize_device_info(struct device_info **dev_info);
76+
77+
void update_device_infos(
78+
unsigned int num_devs,
79+
struct device_info *dev_info);
80+
81+
void clean_device_info(unsigned int num_devs, struct device_info *dev_info);
82+
83+
#endif // __EXTRACT_GPUINFO_H_

include/get_process_info.h

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
*
3+
* Copyright (C) 2017 Maxime Schmitt <[email protected]>
4+
*
5+
* This file is part of Nvtop.
6+
*
7+
* Nvtop is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* Nvtop is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with nvtop. If not, see <http://www.gnu.org/licenses/>.
19+
*
20+
*/
21+
22+
#ifndef __GET_PROCESS_INFO_H_
23+
#define __GET_PROCESS_INFO_H_
24+
25+
#include <stdlib.h>
26+
27+
void get_username_from_pid(pid_t pid, size_t size_buffer, char *buffer);
28+
29+
#endif // __GET_PROCESS_INFO_H_

0 commit comments

Comments
 (0)