github.com/cloud-foundations/dominator@v0.0.0-20221004181915-6e4fee580046/cmd/imageserver/html.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"syscall"
     7  	"time"
     8  
     9  	"github.com/Cloud-Foundations/Dominator/lib/format"
    10  	"github.com/Cloud-Foundations/tricorder/go/tricorder"
    11  	"github.com/Cloud-Foundations/tricorder/go/tricorder/units"
    12  )
    13  
    14  var statisticsComputeBucketer *tricorder.Bucketer
    15  var statisticsComputeCpuTimeDistribution *tricorder.CumulativeDistribution
    16  
    17  func init() {
    18  	statisticsComputeBucketer = tricorder.NewGeometricBucketer(0.1, 100e3)
    19  	statisticsComputeCpuTimeDistribution =
    20  		statisticsComputeBucketer.NewCumulativeDistribution()
    21  	tricorder.RegisterMetric("/statistics-compute-cputime",
    22  		statisticsComputeCpuTimeDistribution,
    23  		units.Millisecond, "statistics compute CPU time")
    24  
    25  }
    26  
    27  func (imageObjectServers *imageObjectServersType) WriteHtml(writer io.Writer) {
    28  	// TODO(rgooch): These statistics should be cached and the cache invalidated
    29  	//               when images and objects are added/deleted.
    30  	var rusageStart, rusageStop syscall.Rusage
    31  	syscall.Getrusage(syscall.RUSAGE_SELF, &rusageStart)
    32  	objectsMap := imageObjectServers.objSrv.ListObjectSizes()
    33  	var totalBytes uint64
    34  	for _, bytes := range objectsMap {
    35  		totalBytes += bytes
    36  	}
    37  	numObjects := len(objectsMap)
    38  	imageObjectServers.objSrv.WriteHtml(writer)
    39  	numUnreferencedObjects, unreferencedBytes :=
    40  		imageObjectServers.imdb.GetUnreferencedObjectsStatistics()
    41  	unreferencedObjectsPercent := 0.0
    42  	if numObjects > 0 {
    43  		unreferencedObjectsPercent =
    44  			100.0 * float64(numUnreferencedObjects) / float64(numObjects)
    45  	}
    46  	unreferencedBytesPercent := 0.0
    47  	if totalBytes > 0 {
    48  		unreferencedBytesPercent =
    49  			100.0 * float64(unreferencedBytes) / float64(totalBytes)
    50  	}
    51  	syscall.Getrusage(syscall.RUSAGE_SELF, &rusageStop)
    52  	statisticsComputeCpuTimeDistribution.Add(time.Duration(
    53  		rusageStop.Utime.Sec)*time.Second +
    54  		time.Duration(rusageStop.Utime.Usec)*time.Microsecond -
    55  		time.Duration(rusageStart.Utime.Sec)*time.Second -
    56  		time.Duration(rusageStart.Utime.Usec)*time.Microsecond)
    57  	fmt.Fprintf(writer,
    58  		"Number of unreferenced objects: %d (%.1f%%), "+
    59  			"consuming %s (%.1f%%)<br>\n",
    60  		numUnreferencedObjects, unreferencedObjectsPercent,
    61  		format.FormatBytes(unreferencedBytes), unreferencedBytesPercent)
    62  }