Skip to content

Commit e8b7336

Browse files
committed
add cpu info.
1 parent b84354c commit e8b7336

6 files changed

Lines changed: 339 additions & 13 deletions

File tree

webrtc-sys/test/CMakeLists.txt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ enable_language(ASM)
66
set(BINARY_NAME "h264_benchmark")
77

88
# Set the C++ standard to C++23.
9-
set(CMAKE_CXX_STANDARD 23)
9+
set(CMAKE_CXX_STANDARD 20)
1010

1111
set(CMAKE_BUILD_TYPE Debug)
1212

@@ -41,24 +41,27 @@ add_definitions(-DWEBRTC_LIBRARY_IMPL)
4141
add_definitions(-DWEBRTC_ENABLE_AVX2)
4242
4343
include_directories(
44-
"${CMAKE_CURRENT_SOURCE_DIR}/../libwebrtc/linux-x64-debug/include"
45-
"${CMAKE_CURRENT_SOURCE_DIR}/../libwebrtc/linux-x64-debug/include/third_party/abseil-cpp"
46-
"${CMAKE_CURRENT_SOURCE_DIR}/../libwebrtc/linux-x64-debug/include/third_party/libyuv/include"
44+
"${CMAKE_CURRENT_SOURCE_DIR}/../libwebrtc/linux-x64-release/include"
45+
"${CMAKE_CURRENT_SOURCE_DIR}/../libwebrtc/linux-x64-release/include/third_party/abseil-cpp"
46+
"${CMAKE_CURRENT_SOURCE_DIR}/../libwebrtc/linux-x64-release/include/third_party/libyuv/include"
4747
"${CMAKE_CURRENT_SOURCE_DIR}/../src/nvidia/NvCodec/include"
4848
"${CMAKE_CURRENT_SOURCE_DIR}/../src/nvidia/NvCodec/NvCodec"
4949
"${CMAKE_CURRENT_SOURCE_DIR}/../src"
5050
)
5151
5252
link_libraries(
53-
"${CMAKE_CURRENT_SOURCE_DIR}/../libwebrtc/linux-x64-debug/lib/libwebrtc.a"
53+
"${CMAKE_CURRENT_SOURCE_DIR}/../libwebrtc/linux-x64-release/lib/libwebrtc.a"
5454
)
5555
56+
find_package (Threads)
57+
5658
add_executable(${BINARY_NAME}
5759
"test_main.cc"
5860
"benchmark.cc"
5961
"benchmark_openh264.cc"
6062
"video_source.cc"
6163
"fileutils.cc"
64+
"cpu/cpu_linux.cc"
6265
6366
#"benchmark_nvidia.cc"
6467
#"../src/nvidia/NvCodec/NvCodec/NvDecoder/NvDecoder.cpp"
@@ -84,3 +87,6 @@ add_executable(${BINARY_NAME}
8487
"../src/vaapi/implib/libva.so.init.c"
8588
"../src/vaapi/implib/libva.so.tramp.S"
8689
)
90+
91+
target_link_libraries(${BINARY_NAME} ${CMAKE_THREAD_LIBS_INIT})
92+
target_link_libraries(${BINARY_NAME} dl)

webrtc-sys/test/benchmark.cc

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020

2121
#include "api/video/i420_buffer.h"
2222
#include "common_video/libyuv/include/webrtc_libyuv.h"
23+
#include "fileutils.h"
2324
#include "modules/video_coding/utility/simulcast_rate_allocator.h"
2425
#include "rtc_base/event.h"
25-
#include "fileutils.h"
2626
#include "video_source.h"
2727

2828
#define SSIM_CALC 0 // by default, don't compute SSIM
@@ -73,9 +73,11 @@ VideoEncodeCompleteCallback::OnEncodedImage(
7373
_encodedBytes += encodedImage.GetEncodedData()->size();
7474

7575
if (_encodedFile != NULL) {
76-
if (fwrite(encodedImage.GetEncodedData()->data(), 1, encodedImage.GetEncodedData()->size(), _encodedFile) !=
77-
encodedImage.GetEncodedData()->size()) {
78-
fprintf(stderr, "Error writing to encoded file %s\n", _test._outname.c_str());
76+
if (fwrite(encodedImage.GetEncodedData()->data(), 1,
77+
encodedImage.GetEncodedData()->size(),
78+
_encodedFile) != encodedImage.GetEncodedData()->size()) {
79+
fprintf(stderr, "Error writing to encoded file %s\n",
80+
_test._outname.c_str());
7981
}
8082
}
8183

@@ -95,7 +97,9 @@ Benchmark::Benchmark(std::string name,
9597
std::string description,
9698
std::string resultsFileName,
9799
std::string codecName)
98-
: _resultsFileName(resultsFileName), _codecName(codecName) {}
100+
: _resultsFileName(resultsFileName),
101+
_codecName(codecName),
102+
_cpu(webrtc::CpuWrapper::CreateCpu()) {}
99103

100104
void Benchmark::Perform() {
101105
std::vector<const VideoSource*> sources;
@@ -195,6 +199,7 @@ void Benchmark::Perform() {
195199
std::cout << " " << totalEncodeTime[k];
196200
_results << "," << totalEncodeTime[k];
197201
}
202+
198203
/*
199204
std::cout << std::endl << "Decode Time[ms]:";
200205
_results << std::endl << "Decode Time[ms]";
@@ -245,6 +250,7 @@ void Benchmark::PerformNormalTest() {
245250
_framecnt = 0;
246251
_encFrameCnt = 0;
247252
_sumEncBytes = 0;
253+
_totalCpuUsage = 0;
248254
_lengthEncFrame = 0;
249255
while (!complete) {
250256
complete = Encode();

webrtc-sys/test/benchmark.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717
#include <queue>
1818
#include <string>
1919

20+
#include "cpu/cpu_linux.h"
2021
#include "modules/include/module_common_types.h"
2122
#include "modules/video_coding/include/video_codec_interface.h"
2223
#include "rtc_base/synchronization/mutex.h"
2324
#include "system_wrappers/include/clock.h"
2425

26+
2527
class VideoSource;
2628
class Benchmark;
2729

@@ -153,9 +155,7 @@ class Benchmark {
153155
return NULL;
154156
};
155157

156-
void UpdateEncodedBytes(int encodedBytes) {
157-
_sumEncBytes += encodedBytes;
158-
}
158+
void UpdateEncodedBytes(int encodedBytes) { _sumEncBytes += encodedBytes; }
159159

160160
const VideoSource* _target;
161161
std::string _resultsFileName;
@@ -169,6 +169,7 @@ class Benchmark {
169169
bool _appendNext = false;
170170
int _framecnt;
171171
int _encFrameCnt;
172+
uint32_t _totalCpuUsage;
172173
double _totalEncodeTime;
173174
double _totalDecodeTime;
174175
double _decodeCompleteTime;
@@ -203,6 +204,7 @@ class Benchmark {
203204
uint64_t _lastDecRefPictureId = 0;
204205
uint64_t _lastDecPictureId = 0;
205206
std::list<fbSignal> _signalPLI;
207+
webrtc::CpuWrapper* _cpu;
206208
};
207209

208210
#endif // WEBRTC_MODULES_VIDEO_CODING_CODECS_TEST_FRAWEWORK_BENCHMARK_H_

webrtc-sys/test/cpu/cpu_linux.cc

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
/*
2+
* Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3+
*
4+
* Use of this source code is governed by a BSD-style license
5+
* that can be found in the LICENSE file in the root of the source
6+
* tree. An additional intellectual property rights grant can be found
7+
* in the file PATENTS. All contributing project authors may
8+
* be found in the AUTHORS file in the root of the source tree.
9+
*/
10+
11+
#include "cpu_linux.h"
12+
13+
#include <stdio.h>
14+
#include <stdlib.h>
15+
#include <string.h>
16+
#include <unistd.h>
17+
18+
namespace webrtc {
19+
CpuLinux::CpuLinux()
20+
: m_oldBusyTime(0),
21+
m_oldIdleTime(0),
22+
m_oldBusyTimeMulti(NULL),
23+
m_oldIdleTimeMulti(NULL),
24+
m_idleArray(NULL),
25+
m_busyArray(NULL),
26+
m_resultArray(NULL),
27+
m_numCores(0) {
28+
const int result = GetNumCores();
29+
if (result != -1) {
30+
m_numCores = result;
31+
m_oldBusyTimeMulti = new long long[m_numCores];
32+
memset(m_oldBusyTimeMulti, 0, sizeof(long long) * m_numCores);
33+
m_oldIdleTimeMulti = new long long[m_numCores];
34+
memset(m_oldIdleTimeMulti, 0, sizeof(long long) * m_numCores);
35+
m_idleArray = new long long[m_numCores];
36+
memset(m_idleArray, 0, sizeof(long long) * m_numCores);
37+
m_busyArray = new long long[m_numCores];
38+
memset(m_busyArray, 0, sizeof(long long) * m_numCores);
39+
m_resultArray = new uint32_t[m_numCores];
40+
41+
GetData(m_oldBusyTime, m_oldIdleTime, m_busyArray, m_idleArray);
42+
}
43+
}
44+
45+
CpuLinux::~CpuLinux()
46+
{
47+
delete [] m_oldBusyTimeMulti;
48+
delete [] m_oldIdleTimeMulti;
49+
delete [] m_idleArray;
50+
delete [] m_busyArray;
51+
delete [] m_resultArray;
52+
}
53+
54+
int32_t CpuLinux::CpuUsage()
55+
{
56+
uint32_t dummy = 0;
57+
uint32_t* dummyArray = NULL;
58+
return CpuUsageMultiCore(dummy, dummyArray);
59+
}
60+
61+
int32_t CpuLinux::CpuUsageMultiCore(uint32_t& numCores,
62+
uint32_t*& coreArray)
63+
{
64+
coreArray = m_resultArray;
65+
numCores = m_numCores;
66+
long long busy = 0;
67+
long long idle = 0;
68+
if (GetData(busy, idle, m_busyArray, m_idleArray) != 0)
69+
return -1;
70+
71+
long long deltaBusy = busy - m_oldBusyTime;
72+
long long deltaIdle = idle - m_oldIdleTime;
73+
m_oldBusyTime = busy;
74+
m_oldIdleTime = idle;
75+
76+
int retVal = -1;
77+
if (deltaBusy + deltaIdle == 0)
78+
{
79+
retVal = 0;
80+
}
81+
else
82+
{
83+
retVal = (int)(100 * (deltaBusy) / (deltaBusy + deltaIdle));
84+
}
85+
86+
if (coreArray == NULL)
87+
{
88+
return retVal;
89+
}
90+
91+
for (int32_t i = 0; i < m_numCores; i++)
92+
{
93+
deltaBusy = m_busyArray[i] - m_oldBusyTimeMulti[i];
94+
deltaIdle = m_idleArray[i] - m_oldIdleTimeMulti[i];
95+
m_oldBusyTimeMulti[i] = m_busyArray[i];
96+
m_oldIdleTimeMulti[i] = m_idleArray[i];
97+
if(deltaBusy + deltaIdle == 0)
98+
{
99+
coreArray[i] = 0;
100+
}
101+
else
102+
{
103+
coreArray[i] = (int)(100 * (deltaBusy) / (deltaBusy+deltaIdle));
104+
}
105+
}
106+
return retVal;
107+
}
108+
109+
110+
int CpuLinux::GetData(long long& busy, long long& idle, long long*& busyArray,
111+
long long*& idleArray)
112+
{
113+
FILE* fp = fopen("/proc/stat", "r");
114+
if (!fp)
115+
{
116+
return -1;
117+
}
118+
119+
char line[100];
120+
if (fgets(line, 100, fp) == NULL) {
121+
fclose(fp);
122+
return -1;
123+
}
124+
char firstWord[100];
125+
if (sscanf(line, "%s ", firstWord) != 1) {
126+
fclose(fp);
127+
return -1;
128+
}
129+
if (strncmp(firstWord, "cpu", 3) != 0) {
130+
fclose(fp);
131+
return -1;
132+
}
133+
char sUser[100];
134+
char sNice[100];
135+
char sSystem[100];
136+
char sIdle[100];
137+
if (sscanf(line, "%s %s %s %s %s ",
138+
firstWord, sUser, sNice, sSystem, sIdle) != 5) {
139+
fclose(fp);
140+
return -1;
141+
}
142+
long long luser = atoll(sUser);
143+
long long lnice = atoll(sNice);
144+
long long lsystem = atoll(sSystem);
145+
long long lidle = atoll (sIdle);
146+
147+
busy = luser + lnice + lsystem;
148+
idle = lidle;
149+
for (int32_t i = 0; i < m_numCores; i++)
150+
{
151+
if (fgets(line, 100, fp) == NULL) {
152+
fclose(fp);
153+
return -1;
154+
}
155+
if (sscanf(line, "%s %s %s %s %s ", firstWord, sUser, sNice, sSystem,
156+
sIdle) != 5) {
157+
fclose(fp);
158+
return -1;
159+
}
160+
luser = atoll(sUser);
161+
lnice = atoll(sNice);
162+
lsystem = atoll(sSystem);
163+
lidle = atoll (sIdle);
164+
busyArray[i] = luser + lnice + lsystem;
165+
idleArray[i] = lidle;
166+
}
167+
fclose(fp);
168+
return 0;
169+
}
170+
171+
int CpuLinux::GetNumCores()
172+
{
173+
FILE* fp = fopen("/proc/stat", "r");
174+
if (!fp)
175+
{
176+
return -1;
177+
}
178+
// Skip first line
179+
char line[100];
180+
if (!fgets(line, 100, fp))
181+
{
182+
fclose(fp);
183+
return -1;
184+
}
185+
int numCores = -1;
186+
char firstWord[100];
187+
do
188+
{
189+
numCores++;
190+
if (fgets(line, 100, fp))
191+
{
192+
if (sscanf(line, "%s ", firstWord) != 1) {
193+
firstWord[0] = '\0';
194+
}
195+
} else {
196+
break;
197+
}
198+
} while (strncmp(firstWord, "cpu", 3) == 0);
199+
fclose(fp);
200+
return numCores;
201+
}
202+
203+
CpuWrapper* CpuWrapper::CreateCpu()
204+
{
205+
return new CpuLinux();
206+
}
207+
208+
} // namespace webrtc

0 commit comments

Comments
 (0)