@@ -4,7 +4,9 @@ package gather
44import  (
55	"context" 
66	"fmt" 
7- 	"runtime" 
7+ 	"os" 
8+ 	"strconv" 
9+ 	"strings" 
810	"time" 
911
1012	"k8s.io/client-go/rest" 
@@ -47,8 +49,9 @@ type GathererFunctionReport struct {
4749type  ArchiveMetadata  struct  {
4850	// info about gathering functions. 
4951	StatusReports  []GathererFunctionReport  `json:"status_reports"` 
50- 	// MemoryAlloc is the amount of memory taken by heap objects after processing the records 
51- 	MemoryAlloc  uint64  `json:"memory_alloc_bytes"` 
52+ 	// MemoryBytesUsage is the number of bytes of memory used by the container. The number is obtained 
53+ 	// from cgroups and is related to the Prometheus metric with the same name. 
54+ 	MemoryBytesUsage  uint64  `json:"container_memory_bytes_usage"` 
5255	// Uptime is the number of seconds from the program start till the point when metadata was created 
5356	Uptime  float64  `json:"uptime_seconds"` 
5457	// IsGlobalObfuscationEnabled shows if obfuscation(hiding IPs and cluster domain) is enabled 
@@ -192,20 +195,31 @@ func recordGatheringFunctionResult(
192195	}, allErrors 
193196}
194197
198+ func  readMemoryUsage () (int , error ) {
199+ 	b , err  :=  os .ReadFile ("/sys/fs/cgroup/memory/memory.usage_in_bytes" )
200+ 	if  err  !=  nil  {
201+ 		return  0 , err 
202+ 	}
203+ 	memUsage  :=  strings .ReplaceAll (string (b ), "\n " , "" )
204+ 	return  strconv .Atoi (memUsage )
205+ }
206+ 
195207// RecordArchiveMetadata records info about archive and gatherers' reports 
196208func  RecordArchiveMetadata (
197209	functionReports  []GathererFunctionReport ,
198210	rec  recorder.Interface ,
199211	anonymizer  * anonymization.Anonymizer ,
200212) error  {
201- 	var  m  runtime.MemStats 
202- 	runtime .ReadMemStats (& m )
213+ 	memUsage , err  :=  readMemoryUsage ()
214+ 	if  err  !=  nil  {
215+ 		klog .Warningf ("can't read cgroups memory usage data: %v" , err )
216+ 	}
203217
204218	archiveMetadata  :=  record.Record {
205219		Name : recorder .MetadataRecordName ,
206220		Item : record.JSONMarshaller {Object : ArchiveMetadata {
207221			StatusReports :              functionReports ,
208- 			MemoryAlloc :                 m . HeapAlloc ,
222+ 			MemoryBytesUsage :           uint64 ( memUsage ) ,
209223			Uptime :                     time .Since (programStartTime ).Truncate (time .Millisecond ).Seconds (),
210224			IsGlobalObfuscationEnabled : anonymizer .IsObfuscationEnabled (),
211225		}},
0 commit comments