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