github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/plugins/memory_app/actor.go (about)

     1  // This file is part of the Smart Home
     2  // Program complex distribution https://github.com/e154/smart-home
     3  // Copyright (C) 2016-2023, Filippov Alex
     4  //
     5  // This library is free software: you can redistribute it and/or
     6  // modify it under the terms of the GNU Lesser General Public
     7  // License as published by the Free Software Foundation; either
     8  // version 3 of the License, or (at your option) any later version.
     9  //
    10  // This library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13  // Library General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public
    16  // License along with this library.  If not, see
    17  // <https://www.gnu.org/licenses/>.
    18  
    19  package memory_app
    20  
    21  import (
    22  	"runtime"
    23  	"sync"
    24  	"time"
    25  
    26  	m "github.com/e154/smart-home/models"
    27  	"github.com/e154/smart-home/system/supervisor"
    28  )
    29  
    30  // Actor ...
    31  type Actor struct {
    32  	*supervisor.BaseActor
    33  	updateLock *sync.Mutex
    34  }
    35  
    36  // NewActor ...
    37  func NewActor(entity *m.Entity,
    38  	service supervisor.Service) *Actor {
    39  
    40  	actor := &Actor{
    41  		BaseActor:  supervisor.NewBaseActor(entity, service),
    42  		updateLock: &sync.Mutex{},
    43  	}
    44  
    45  	if entity != nil {
    46  		actor.Metric = entity.Metrics
    47  	}
    48  
    49  	return actor
    50  }
    51  
    52  func (e *Actor) Destroy() {
    53  
    54  }
    55  
    56  func (e *Actor) selfUpdate() {
    57  
    58  	e.updateLock.Lock()
    59  	defer e.updateLock.Unlock()
    60  
    61  	var s runtime.MemStats
    62  	runtime.ReadMemStats(&s)
    63  
    64  	e.AttrMu.Lock()
    65  	e.Attrs[AttrAlloc].Value = s.Alloc
    66  	e.Attrs[AttrHeapAlloc].Value = s.HeapAlloc
    67  	e.Attrs[AttrTotalAlloc].Value = s.TotalAlloc
    68  	e.Attrs[AttrSys].Value = s.Sys
    69  	e.Attrs[AttrNumGC].Value = s.NumGC
    70  	e.Attrs[AttrLastGC].Value = time.Unix(0, int64(s.LastGC)).UTC()
    71  	e.AttrMu.Unlock()
    72  
    73  	//e.SetMetric(e.Id, "memory_app", map[string]float32{
    74  	//	"total_alloc": float32(s.TotalAlloc),
    75  	//})
    76  
    77  	e.SaveState(false, false)
    78  }