blob: 874b035c4008c8fe489b10beeaf180d602e7cf1f [file] [log] [blame]
Stephane Barbarieec0919b2018-09-05 14:14:29 -04001/*
2 * Copyright 2018-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Stephane Barbariedc5022d2018-11-19 15:21:44 -050016
Stephane Barbarieec0919b2018-09-05 14:14:29 -040017package model
18
19import (
Stephane Barbarieec0919b2018-09-05 14:14:29 -040020 "github.com/opencord/voltha-go/common/log"
khenaidoob9203542018-09-17 22:56:37 -040021 "sync"
Stephane Barbarieec0919b2018-09-05 14:14:29 -040022)
23
Stephane Barbariedc5022d2018-11-19 15:21:44 -050024// Profiling is used to store performance details collected at runtime
Stephane Barbarieec0919b2018-09-05 14:14:29 -040025type profiling struct {
Stephane Barbariedc5022d2018-11-19 15:21:44 -050026 sync.RWMutex
Stephane Barbarieec0919b2018-09-05 14:14:29 -040027 DatabaseRetrieveTime float64
28 DatabaseRetrieveCount int
29 InMemoryModelTime float64
30 InMemoryModelCount int
31 InMemoryProcessTime float64
32 DatabaseStoreTime float64
Stephane Barbariea188d942018-10-16 16:43:04 -040033 InMemoryLockTime float64
34 InMemoryLockCount int
Stephane Barbarieec0919b2018-09-05 14:14:29 -040035}
36
Stephane Barbariedc5022d2018-11-19 15:21:44 -050037var profilingInstance *profiling
38var profilingOnce sync.Once
Stephane Barbarieec0919b2018-09-05 14:14:29 -040039
Stephane Barbariedc5022d2018-11-19 15:21:44 -050040// GetProfiling returns a singleton instance of the Profiling structure
Stephane Barbarieec0919b2018-09-05 14:14:29 -040041func GetProfiling() *profiling {
Stephane Barbariedc5022d2018-11-19 15:21:44 -050042 profilingOnce.Do(func() {
43 profilingInstance = &profiling{}
Stephane Barbarieec0919b2018-09-05 14:14:29 -040044 })
Stephane Barbariedc5022d2018-11-19 15:21:44 -050045 return profilingInstance
Stephane Barbarieec0919b2018-09-05 14:14:29 -040046}
47
Stephane Barbariedc5022d2018-11-19 15:21:44 -050048// AddToDatabaseRetrieveTime appends a time period to retrieve data from the database
Stephane Barbarieec0919b2018-09-05 14:14:29 -040049func (p *profiling) AddToDatabaseRetrieveTime(period float64) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -050050 p.Lock()
51 defer p.Unlock()
52
Stephane Barbarieec0919b2018-09-05 14:14:29 -040053 p.DatabaseRetrieveTime += period
Stephane Barbariedc5022d2018-11-19 15:21:44 -050054 p.DatabaseRetrieveCount++
Stephane Barbarieec0919b2018-09-05 14:14:29 -040055}
Stephane Barbariedc5022d2018-11-19 15:21:44 -050056
57// AddToInMemoryModelTime appends a time period to construct/deconstruct data in memory
Stephane Barbarieec0919b2018-09-05 14:14:29 -040058func (p *profiling) AddToInMemoryModelTime(period float64) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -050059 p.Lock()
60 defer p.Unlock()
61
Stephane Barbarieec0919b2018-09-05 14:14:29 -040062 p.InMemoryModelTime += period
Stephane Barbariedc5022d2018-11-19 15:21:44 -050063 p.InMemoryModelCount++
Stephane Barbarieec0919b2018-09-05 14:14:29 -040064}
Stephane Barbariedc5022d2018-11-19 15:21:44 -050065
66// AddToInMemoryProcessTime appends a time period to process data
Stephane Barbarieec0919b2018-09-05 14:14:29 -040067func (p *profiling) AddToInMemoryProcessTime(period float64) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -050068 p.Lock()
69 defer p.Unlock()
70
Stephane Barbarieec0919b2018-09-05 14:14:29 -040071 p.InMemoryProcessTime += period
72}
Stephane Barbariedc5022d2018-11-19 15:21:44 -050073
74// AddToDatabaseStoreTime appends a time period to store data in the database
Stephane Barbarieec0919b2018-09-05 14:14:29 -040075func (p *profiling) AddToDatabaseStoreTime(period float64) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -050076 p.Lock()
77 defer p.Unlock()
78
Stephane Barbarieec0919b2018-09-05 14:14:29 -040079 p.DatabaseStoreTime += period
80}
81
Stephane Barbariedc5022d2018-11-19 15:21:44 -050082// AddToInMemoryLockTime appends a time period when a code block was locked
Stephane Barbariea188d942018-10-16 16:43:04 -040083func (p *profiling) AddToInMemoryLockTime(period float64) {
Stephane Barbariedc5022d2018-11-19 15:21:44 -050084 p.Lock()
85 defer p.Unlock()
86
Stephane Barbariea188d942018-10-16 16:43:04 -040087 p.InMemoryLockTime += period
Stephane Barbariedc5022d2018-11-19 15:21:44 -050088 p.InMemoryLockCount++
Stephane Barbariea188d942018-10-16 16:43:04 -040089}
90
Stephane Barbariedc5022d2018-11-19 15:21:44 -050091// Reset initializes the profile counters
Stephane Barbariea188d942018-10-16 16:43:04 -040092func (p *profiling) Reset() {
Stephane Barbariedc5022d2018-11-19 15:21:44 -050093 p.Lock()
94 defer p.Unlock()
95
Stephane Barbariea188d942018-10-16 16:43:04 -040096 p.DatabaseRetrieveTime = 0
97 p.DatabaseRetrieveCount = 0
98 p.InMemoryModelTime = 0
99 p.InMemoryModelCount = 0
100 p.InMemoryProcessTime = 0
101 p.DatabaseStoreTime = 0
102 p.InMemoryLockTime = 0
103 p.InMemoryLockCount = 0
104}
105
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500106// Report will provide the current profile counter status
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400107func (p *profiling) Report() {
Stephane Barbariedc5022d2018-11-19 15:21:44 -0500108 p.Lock()
109 defer p.Unlock()
110
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400111 log.Infof("[ Profiling Report ]")
112 log.Infof("Database Retrieval : %f", p.DatabaseRetrieveTime)
113 log.Infof("Database Retrieval Count : %d", p.DatabaseRetrieveCount)
114 log.Infof("Avg Database Retrieval : %f", p.DatabaseRetrieveTime/float64(p.DatabaseRetrieveCount))
115 log.Infof("In-Memory Modeling : %f", p.InMemoryModelTime)
116 log.Infof("In-Memory Modeling Count: %d", p.InMemoryModelCount)
117 log.Infof("Avg In-Memory Modeling : %f", p.InMemoryModelTime/float64(p.InMemoryModelCount))
Stephane Barbariea188d942018-10-16 16:43:04 -0400118 log.Infof("In-Memory Locking : %f", p.InMemoryLockTime)
119 log.Infof("In-Memory Locking Count: %d", p.InMemoryLockCount)
120 log.Infof("Avg In-Memory Locking : %f", p.InMemoryLockTime/float64(p.InMemoryLockCount))
Stephane Barbarieec0919b2018-09-05 14:14:29 -0400121
122}