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 ( |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 20 | "github.com/opencord/voltha-go/common/log" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 21 | "sync" |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 22 | ) |
| 23 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 24 | // Profiling is used to store performance details collected at runtime |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 25 | type profiling struct { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 26 | sync.RWMutex |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 27 | DatabaseRetrieveTime float64 |
| 28 | DatabaseRetrieveCount int |
| 29 | InMemoryModelTime float64 |
| 30 | InMemoryModelCount int |
| 31 | InMemoryProcessTime float64 |
| 32 | DatabaseStoreTime float64 |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 33 | InMemoryLockTime float64 |
| 34 | InMemoryLockCount int |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 35 | } |
| 36 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 37 | var profilingInstance *profiling |
| 38 | var profilingOnce sync.Once |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 39 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 40 | // GetProfiling returns a singleton instance of the Profiling structure |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 41 | func GetProfiling() *profiling { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 42 | profilingOnce.Do(func() { |
| 43 | profilingInstance = &profiling{} |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 44 | }) |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 45 | return profilingInstance |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 46 | } |
| 47 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 48 | // AddToDatabaseRetrieveTime appends a time period to retrieve data from the database |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 49 | func (p *profiling) AddToDatabaseRetrieveTime(period float64) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 50 | p.Lock() |
| 51 | defer p.Unlock() |
| 52 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 53 | p.DatabaseRetrieveTime += period |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 54 | p.DatabaseRetrieveCount++ |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 55 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 56 | |
| 57 | // AddToInMemoryModelTime appends a time period to construct/deconstruct data in memory |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 58 | func (p *profiling) AddToInMemoryModelTime(period float64) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 59 | p.Lock() |
| 60 | defer p.Unlock() |
| 61 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 62 | p.InMemoryModelTime += period |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 63 | p.InMemoryModelCount++ |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 64 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 65 | |
| 66 | // AddToInMemoryProcessTime appends a time period to process data |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 67 | func (p *profiling) AddToInMemoryProcessTime(period float64) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 68 | p.Lock() |
| 69 | defer p.Unlock() |
| 70 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 71 | p.InMemoryProcessTime += period |
| 72 | } |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 73 | |
| 74 | // AddToDatabaseStoreTime appends a time period to store data in the database |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 75 | func (p *profiling) AddToDatabaseStoreTime(period float64) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 76 | p.Lock() |
| 77 | defer p.Unlock() |
| 78 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 79 | p.DatabaseStoreTime += period |
| 80 | } |
| 81 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 82 | // AddToInMemoryLockTime appends a time period when a code block was locked |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 83 | func (p *profiling) AddToInMemoryLockTime(period float64) { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 84 | p.Lock() |
| 85 | defer p.Unlock() |
| 86 | |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 87 | p.InMemoryLockTime += period |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 88 | p.InMemoryLockCount++ |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 89 | } |
| 90 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 91 | // Reset initializes the profile counters |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 92 | func (p *profiling) Reset() { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 93 | p.Lock() |
| 94 | defer p.Unlock() |
| 95 | |
Stephane Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 96 | 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 Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 106 | // Report will provide the current profile counter status |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 107 | func (p *profiling) Report() { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 108 | p.Lock() |
| 109 | defer p.Unlock() |
| 110 | |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 111 | 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 Barbarie | a188d94 | 2018-10-16 16:43:04 -0400 | [diff] [blame] | 118 | 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 Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 121 | |
| 122 | } |