Line | Branch | Exec | Source |
---|---|---|---|
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 | |||
24 | #include "phoenix_hardware_characteristics.h" | ||
25 | |||
26 | ///Get the ratio of RAM used when this function is called | ||
27 | /** @return ratio of the used RAM at this time, between 0 and 1 | ||
28 | */ | ||
29 | 1 | float phoenix_hardware_usedRAM(){ | |
30 | struct sysinfo memInfo; | ||
31 | 1 | 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 | 1 | size_t totalPhysMemRAM = memInfo.totalram; | |
44 | //Multiply in next statement to avoid int overflow on right hand side... | ||
45 | 1 | totalPhysMemRAM *= memInfo.mem_unit; | |
46 | |||
47 | //Physical memory currently used | ||
48 | 1 | size_t physMemUsed = memInfo.totalram - memInfo.freeram; | |
49 | //Multiply in next statement to avoid int overflow on right hand side... | ||
50 | 1 | physMemUsed *= memInfo.mem_unit; | |
51 | |||
52 | 1 | return (float)(((double)physMemUsed)/((double)totalPhysMemRAM)); | |
53 | } | ||
54 | |||
55 | ///Get the total amount of RAM (in GB) of the current host | ||
56 | /** @return total amount of RAM (in GB) of the current host | ||
57 | */ | ||
58 | 1 | float phoenix_hardware_totalRAM(){ | |
59 | struct sysinfo memInfo; | ||
60 | 1 | sysinfo(&memInfo); | |
61 | //Get the total RAM amount | ||
62 | 1 | size_t totalPhysMemRAM = memInfo.totalram; | |
63 | //Multiply in next statement to avoid int overflow on right hand side... | ||
64 | 1 | totalPhysMemRAM *= memInfo.mem_unit; | |
65 | 1 | float totalRamGB(totalPhysMemRAM/1e9); | |
66 | 1 | return totalRamGB; | |
67 | } | ||
68 | |||
69 | ///Get the host name and the IP address and the current host | ||
70 | /** @param[out] hostName : name of the current host | ||
71 | * @param[out] ipAddress : ip address of the current host | ||
72 | */ | ||
73 | 1 | void phoenix_hardware_hostNameIp(std::string & hostName, std::string & ipAddress){ | |
74 |
1/1✓ Branch 0 (2→3) taken 1 times.
|
1 | hostName = ""; |
75 |
1/1✓ Branch 0 (3→4) taken 1 times.
|
1 | ipAddress = ""; |
76 | char hostbuffer[256]; | ||
77 | struct hostent *host_entry; | ||
78 | int hostNameId; | ||
79 | |||
80 | // To retrieve hostname | ||
81 | 1 | hostNameId = gethostname(hostbuffer, sizeof(hostbuffer)); | |
82 |
1/2✗ Branch 0 (5→6) not taken.
✓ Branch 1 (5→7) taken 1 times.
|
1 | if(hostNameId == -1){return;} |
83 | |||
84 | // To retrieve host information | ||
85 |
1/1✓ Branch 0 (7→8) taken 1 times.
|
1 | host_entry = gethostbyname(hostbuffer); |
86 |
1/2✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→10) taken 1 times.
|
1 | if(host_entry == NULL){return;} |
87 | //Set the host name | ||
88 |
1/1✓ Branch 0 (10→11) taken 1 times.
|
1 | hostName = hostbuffer; |
89 | //Convert an Internet network address into ASCII string | ||
90 |
1/1✓ Branch 0 (12→13) taken 1 times.
|
1 | ipAddress = inet_ntoa(*((struct in_addr*) host_entry->h_addr_list[0])); |
91 | } | ||
92 | |||
93 | ///Get the number of cores on the current host | ||
94 | /** @return number of cores on the current host | ||
95 | */ | ||
96 | 1 | size_t phoenix_hardware_nbCore(){ | |
97 | //may return 0 when not able to detect | ||
98 | 1 | unsigned int processor_count = std::thread::hardware_concurrency(); | |
99 |
1/2✗ Branch 0 (3→4) not taken.
✓ Branch 1 (3→8) taken 1 times.
|
1 | 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 | 1 | return processor_count; | |
106 | } | ||
107 | |||
108 |