Stats API: added GetMemory()
[govpp.git] / core / stats.go
index 2a9e964..f2da494 100644 (file)
@@ -39,6 +39,10 @@ const (
 
        CounterStatsPrefix = "/err/"
 
+       MemoryStatPrefix  = "/mem/statseg"
+       MemoryStats_Total = "total"
+       MemoryStats_Used  = "used"
+
        InterfaceStatsPrefix         = "/if/"
        InterfaceStats_Names         = InterfaceStatsPrefix + "names"
        InterfaceStats_Drops         = InterfaceStatsPrefix + "drops"
@@ -80,6 +84,7 @@ type StatsConnection struct {
        ifaceStatsData *adapter.StatDir
        sysStatsData   *adapter.StatDir
        bufStatsData   *adapter.StatDir
+       memStatsData   *adapter.StatDir
 }
 
 func newStatsConnection(stats adapter.StatsAPI) *StatsConnection {
@@ -88,7 +93,7 @@ func newStatsConnection(stats adapter.StatsAPI) *StatsConnection {
        }
 }
 
-// Connect connects to Stats API using specified adapter and returns a connection handle.
+// ConnectStats connects to Stats API using specified adapter and returns a connection handle.
 // This call blocks until it is either connected, or an error occurs.
 // Only one connection attempt will be performed.
 func ConnectStats(stats adapter.StatsAPI) (*StatsConnection, error) {
@@ -177,7 +182,7 @@ func (c *StatsConnection) updateStats(statDir **adapter.StatDir, patterns ...str
        return err
 }
 
-// UpdateSystemStats retrieves VPP system stats.
+// GetSystemStats retrieves VPP system stats.
 func (c *StatsConnection) GetSystemStats(sysStats *api.SystemStats) (err error) {
        if err := c.updateStats(&c.sysStatsData, SystemStatsPrefix); err != nil {
                return err
@@ -198,6 +203,9 @@ func (c *StatsConnection) GetSystemStats(sysStats *api.SystemStats) (err error)
                        if ss, ok := stat.Data.(adapter.SimpleCounterStat); ok {
                                vals = make([]uint64, len(ss))
                                for w := range ss {
+                                       if ss[w] == nil {
+                                               continue
+                                       }
                                        vals[w] = uint64(ss[w][0])
                                }
                        }
@@ -468,3 +476,25 @@ func (c *StatsConnection) GetBufferStats(bufStats *api.BufferStats) (err error)
 
        return nil
 }
+
+func (c *StatsConnection) GetMemoryStats(memStats *api.MemoryStats) (err error) {
+       if err := c.updateStats(&c.memStatsData, MemoryStatPrefix); err != nil {
+               return err
+       }
+
+       for _, stat := range c.memStatsData.Entries {
+               _, f := path.Split(string(stat.Name))
+               var val float64
+               m, ok := stat.Data.(adapter.ScalarStat)
+               if ok {
+                       val = float64(m)
+               }
+               switch f {
+               case MemoryStats_Total:
+                       memStats.Total = val
+               case MemoryStats_Used:
+                       memStats.Used = val
+               }
+       }
+       return nil
+}