@@ -254,6 +254,71 @@ public void testCgroupProbeWithMissingMemory() {
254
254
assertNull (cgroup );
255
255
}
256
256
257
+ public void testGetTotalMemFromProcMeminfo () throws Exception {
258
+ // missing MemTotal line
259
+ var meminfoLines = Arrays .asList (
260
+ "MemFree: 8467692 kB" ,
261
+ "MemAvailable: 39646240 kB" ,
262
+ "Buffers: 4699504 kB" ,
263
+ "Cached: 23290380 kB" ,
264
+ "SwapCached: 0 kB" ,
265
+ "Active: 43637908 kB" ,
266
+ "Inactive: 8130280 kB"
267
+ );
268
+ OsProbe probe = buildStubOsProbe (true , "" , List .of (), meminfoLines );
269
+ assertThat (probe .getTotalMemFromProcMeminfo (), equalTo (0L ));
270
+
271
+ // MemTotal line with invalid value
272
+ meminfoLines = Arrays .asList (
273
+ "MemTotal: invalid kB" ,
274
+ "MemFree: 8467692 kB" ,
275
+ "MemAvailable: 39646240 kB" ,
276
+ "Buffers: 4699504 kB" ,
277
+ "Cached: 23290380 kB" ,
278
+ "SwapCached: 0 kB" ,
279
+ "Active: 43637908 kB" ,
280
+ "Inactive: 8130280 kB"
281
+ );
282
+ probe = buildStubOsProbe (true , "" , List .of (), meminfoLines );
283
+ assertThat (probe .getTotalMemFromProcMeminfo (), equalTo (0L ));
284
+
285
+ // MemTotal line with invalid unit
286
+ meminfoLines = Arrays .asList (
287
+ "MemTotal: 39646240 MB" ,
288
+ "MemFree: 8467692 kB" ,
289
+ "MemAvailable: 39646240 kB" ,
290
+ "Buffers: 4699504 kB" ,
291
+ "Cached: 23290380 kB" ,
292
+ "SwapCached: 0 kB" ,
293
+ "Active: 43637908 kB" ,
294
+ "Inactive: 8130280 kB"
295
+ );
296
+ probe = buildStubOsProbe (true , "" , List .of (), meminfoLines );
297
+ assertThat (probe .getTotalMemFromProcMeminfo (), equalTo (0L ));
298
+
299
+ // MemTotal line with random valid value
300
+ long memTotalInKb = randomLongBetween (1 , Long .MAX_VALUE / 1024L );
301
+ meminfoLines = Arrays .asList (
302
+ "MemTotal: " + memTotalInKb + " kB" ,
303
+ "MemFree: 8467692 kB" ,
304
+ "MemAvailable: 39646240 kB" ,
305
+ "Buffers: 4699504 kB" ,
306
+ "Cached: 23290380 kB" ,
307
+ "SwapCached: 0 kB" ,
308
+ "Active: 43637908 kB" ,
309
+ "Inactive: 8130280 kB"
310
+ );
311
+ probe = buildStubOsProbe (true , "" , List .of (), meminfoLines );
312
+ assertThat (probe .getTotalMemFromProcMeminfo (), equalTo (memTotalInKb * 1024L ));
313
+ }
314
+
315
+ public void testGetTotalMemoryOnDebian8 () throws Exception {
316
+ // tests the workaround for JDK bug on debian8: https://github.com/elastic/elasticsearch/issues/67089#issuecomment-756114654
317
+ final OsProbe osProbe = new OsProbe ();
318
+ assumeTrue ("runs only on Debian 8" , osProbe .isDebian8 ());
319
+ assertThat (osProbe .getTotalPhysicalMemorySize (), greaterThan (0L ));
320
+ }
321
+
257
322
private static List <String > getProcSelfGroupLines (String hierarchy ) {
258
323
return Arrays .asList (
259
324
"10:freezer:/" ,
@@ -282,12 +347,14 @@ private static OsProbe buildStubOsProbe(final boolean areCgroupStatsAvailable, f
282
347
* @param areCgroupStatsAvailable whether or not cgroup data is available. Normally OsProbe establishes this for itself.
283
348
* @param hierarchy a mock value used to generate a cgroup hierarchy.
284
349
* @param procSelfCgroupLines the lines that will be used as the content of <code>/proc/self/cgroup</code>
350
+ * @param procMeminfoLines lines that will be used as the content of <code>/proc/meminfo</code>
285
351
* @return a test instance
286
352
*/
287
353
private static OsProbe buildStubOsProbe (
288
354
final boolean areCgroupStatsAvailable ,
289
355
final String hierarchy ,
290
- List <String > procSelfCgroupLines
356
+ List <String > procSelfCgroupLines ,
357
+ List <String > procMeminfoLines
291
358
) {
292
359
return new OsProbe () {
293
360
@ Override
@@ -338,6 +405,20 @@ String readSysFsCgroupMemoryUsageInBytes(String controlGroup) {
338
405
boolean areCgroupStatsAvailable () {
339
406
return areCgroupStatsAvailable ;
340
407
}
408
+
409
+ @ Override
410
+ List <String > readProcMeminfo () throws IOException {
411
+ return procMeminfoLines ;
412
+ }
341
413
};
342
414
}
415
+
416
+ private static OsProbe buildStubOsProbe (
417
+ final boolean areCgroupStatsAvailable ,
418
+ final String hierarchy ,
419
+ List <String > procSelfCgroupLines
420
+ ) {
421
+ return buildStubOsProbe (areCgroupStatsAvailable , hierarchy , procSelfCgroupLines , List .of ());
422
+ }
423
+
343
424
}
0 commit comments