PhoenixHardware  0.2.0
Tools to get hardware information
Loading...
Searching...
No Matches
phoenix_hardware_characteristics.cpp
Go to the documentation of this file.
1/***************************************
2 Auteur : Pierre Aubert
3 Mail : pierre.aubert@lapp.in2p3.fr
4 Licence : CeCILL-C
5****************************************/
6
7
8
9#include <stdio.h>
10#include <stdlib.h>
11#include <unistd.h>
12#include <errno.h>
13#include <netdb.h>
14
15#include <sys/types.h>
16#include <sys/sysinfo.h>
17#include <sys/socket.h>
18
19#include <netinet/in.h>
20#include <arpa/inet.h>
21
22#include <thread>
23
25
27
30 struct sysinfo memInfo;
31 sysinfo(&memInfo);
32// long long totalVirtualMem = memInfo.totalram;
33// //Add other values in next statement to avoid int overflow on right hand side...
34// totalVirtualMem += memInfo.totalswap;
35// totalVirtualMem *= memInfo.mem_unit;
36//
37// long long virtualMemUsed = memInfo.totalram - memInfo.freeram;
38// //Add other values in next statement to avoid int overflow on right hand side...
39// virtualMemUsed += memInfo.totalswap - memInfo.freeswap;
40// virtualMemUsed *= memInfo.mem_unit;
41
42 //Get the total RAM amount
43 size_t totalPhysMemRAM = memInfo.totalram;
44 //Multiply in next statement to avoid int overflow on right hand side...
45 totalPhysMemRAM *= memInfo.mem_unit;
46
47 //Physical memory currently used
48 size_t physMemUsed = memInfo.totalram - memInfo.freeram;
49 //Multiply in next statement to avoid int overflow on right hand side...
50 physMemUsed *= memInfo.mem_unit;
51
52 return (float)(((double)physMemUsed)/((double)totalPhysMemRAM));
53}
54
56
59 struct sysinfo memInfo;
60 sysinfo(&memInfo);
61 //Get the total RAM amount
62 size_t totalPhysMemRAM = memInfo.totalram;
63 //Multiply in next statement to avoid int overflow on right hand side...
64 totalPhysMemRAM *= memInfo.mem_unit;
65 float totalRamGB(totalPhysMemRAM/1e9);
66 return totalRamGB;
67}
68
70
73void phoenix_hardware_hostNameIp(std::string & hostName, std::string & ipAddress){
74 hostName = "";
75 ipAddress = "";
76 char hostbuffer[256];
77 struct hostent *host_entry;
78 int hostNameId;
79
80 // To retrieve hostname
81 hostNameId = gethostname(hostbuffer, sizeof(hostbuffer));
82 if(hostNameId == -1){return;}
83
84 // To retrieve host information
85 host_entry = gethostbyname(hostbuffer);
86 if(host_entry == NULL){return;}
87 //Set the host name
88 hostName = hostbuffer;
89 //Convert an Internet network address into ASCII string
90 ipAddress = inet_ntoa(*((struct in_addr*) host_entry->h_addr_list[0]));
91}
92
94
97 //may return 0 when not able to detect
98 unsigned int processor_count = std::thread::hardware_concurrency();
99 if(processor_count == 0u){
100 //Check an other solution
101 int numCPU = sysconf(_SC_NPROCESSORS_ONLN);
102 if(numCPU == -1){return 0lu;} //if there is a failure, return 0 (undefined)
103 return numCPU;
104 }
105 return processor_count;
106}
107
void phoenix_hardware_hostNameIp(std::string &hostName, std::string &ipAddress)
Get the host name and the IP address and the current host.
float phoenix_hardware_usedRAM()
Get the ratio of RAM used when this function is called.
size_t phoenix_hardware_nbCore()
Get the number of cores on the current host.
float phoenix_hardware_totalRAM()
Get the total amount of RAM (in GB) of the current host.