Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 1 | /* |
| 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 Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 16 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 17 | package model |
| 18 | |
| 19 | import ( |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 20 | "sync" |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 21 | |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 22 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 23 | ) |
| 24 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 25 | // Profiling is used to store performance details collected at runtime |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 26 | type Profiling struct { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 27 | sync.RWMutex |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 28 | DatabaseRetrieveTime float64 |
| 29 | DatabaseRetrieveCount int |
| 30 | InMemoryModelTime float64 |
| 31 | InMemoryModelCount int |
| 32 | InMemoryProcessTime float64 |
| 33 | DatabaseStoreTime float64 |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 34 | InMemoryLockTime float64 |
| 35 | InMemoryLockCount int |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 36 | } |
| 37 | |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 38 | var profilingInstance *Profiling |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 39 | var profilingOnce sync.Once |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 40 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 41 | // GetProfiling returns a singleton instance of the Profiling structure |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 42 | func GetProfiling() *Profiling { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 43 | profilingOnce.Do(func() { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 44 | profilingInstance = &Profiling{} |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 45 | }) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 46 | return profilingInstance |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 47 | } |
| 48 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 49 | // AddToDatabaseRetrieveTime appends a time period to retrieve data from the database |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 50 | func (p *Profiling) AddToDatabaseRetrieveTime(period float64) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 51 | p.Lock() |
| 52 | defer p.Unlock() |
| 53 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 54 | p.DatabaseRetrieveTime += period |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 55 | p.DatabaseRetrieveCount++ |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 56 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 57 | |
| 58 | // AddToInMemoryModelTime appends a time period to construct/deconstruct data in memory |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 59 | func (p *Profiling) AddToInMemoryModelTime(period float64) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 60 | p.Lock() |
| 61 | defer p.Unlock() |
| 62 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 63 | p.InMemoryModelTime += period |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 64 | p.InMemoryModelCount++ |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 65 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 66 | |
| 67 | // AddToInMemoryProcessTime appends a time period to process data |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 68 | func (p *Profiling) AddToInMemoryProcessTime(period float64) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 69 | p.Lock() |
| 70 | defer p.Unlock() |
| 71 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 72 | p.InMemoryProcessTime += period |
| 73 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 74 | |
| 75 | // AddToDatabaseStoreTime appends a time period to store data in the database |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 76 | func (p *Profiling) AddToDatabaseStoreTime(period float64) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 77 | p.Lock() |
| 78 | defer p.Unlock() |
| 79 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 80 | p.DatabaseStoreTime += period |
| 81 | } |
| 82 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 83 | // AddToInMemoryLockTime appends a time period when a code block was locked |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 84 | func (p *Profiling) AddToInMemoryLockTime(period float64) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 85 | p.Lock() |
| 86 | defer p.Unlock() |
| 87 | |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 88 | p.InMemoryLockTime += period |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 89 | p.InMemoryLockCount++ |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 90 | } |
| 91 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 92 | // Reset initializes the profile counters |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 93 | func (p *Profiling) Reset() { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 94 | p.Lock() |
| 95 | defer p.Unlock() |
| 96 | |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 97 | p.DatabaseRetrieveTime = 0 |
| 98 | p.DatabaseRetrieveCount = 0 |
| 99 | p.InMemoryModelTime = 0 |
| 100 | p.InMemoryModelCount = 0 |
| 101 | p.InMemoryProcessTime = 0 |
| 102 | p.DatabaseStoreTime = 0 |
| 103 | p.InMemoryLockTime = 0 |
| 104 | p.InMemoryLockCount = 0 |
| 105 | } |
| 106 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 107 | // Report will provide the current profile counter status |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 108 | func (p *Profiling) Report() { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 109 | p.Lock() |
| 110 | defer p.Unlock() |
| 111 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 112 | log.Infof("[ Profiling Report ]") |
| 113 | log.Infof("Database Retrieval : %f", p.DatabaseRetrieveTime) |
| 114 | log.Infof("Database Retrieval Count : %d", p.DatabaseRetrieveCount) |
| 115 | log.Infof("Avg Database Retrieval : %f", p.DatabaseRetrieveTime/float64(p.DatabaseRetrieveCount)) |
| 116 | log.Infof("In-Memory Modeling : %f", p.InMemoryModelTime) |
| 117 | log.Infof("In-Memory Modeling Count: %d", p.InMemoryModelCount) |
| 118 | log.Infof("Avg In-Memory Modeling : %f", p.InMemoryModelTime/float64(p.InMemoryModelCount)) |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 119 | log.Infof("In-Memory Locking : %f", p.InMemoryLockTime) |
| 120 | log.Infof("In-Memory Locking Count: %d", p.InMemoryLockCount) |
| 121 | log.Infof("Avg In-Memory Locking : %f", p.InMemoryLockTime/float64(p.InMemoryLockCount)) |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 122 | |
| 123 | } |