Няма описание

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. // Copyright 2014 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package profile
  15. import (
  16. "fmt"
  17. "reflect"
  18. "sort"
  19. "strconv"
  20. "strings"
  21. )
  22. // Merge merges all the profiles in profs into a single Profile.
  23. // Returns a new profile independent of the input profiles. The merged
  24. // profile is compacted to eliminate unused samples, locations,
  25. // functions and mappings. Profiles must have identical profile sample
  26. // and period types or the merge will fail. profile.Period of the
  27. // resulting profile will be the maximum of all profiles, and
  28. // profile.TimeNanos will be the earliest nonzero one.
  29. func Merge(srcs []*Profile) (*Profile, error) {
  30. if len(srcs) == 0 {
  31. return nil, fmt.Errorf("no profiles to merge")
  32. }
  33. p, err := combineHeaders(srcs)
  34. if err != nil {
  35. return nil, err
  36. }
  37. pm := &profileMerger{
  38. p: p,
  39. samples: make(map[string]*Sample, len(srcs[0].Sample)),
  40. locations: make(map[string]*Location, len(srcs[0].Location)),
  41. functions: make(map[string]*Function, len(srcs[0].Function)),
  42. mappings: make(map[string]*Mapping, len(srcs[0].Mapping)),
  43. }
  44. for _, src := range srcs {
  45. // Clear the profile-specific hash tables
  46. pm.locationsByID = make(map[uint64]*Location, len(src.Location))
  47. pm.functionsByID = make(map[uint64]*Function, len(src.Function))
  48. pm.mappingsByID = make(map[uint64]mapInfo, len(src.Mapping))
  49. if len(pm.mappings) == 0 && len(src.Mapping) > 0 {
  50. // The Mapping list has the property that the first mapping
  51. // represents the main binary. Take the first Mapping we see,
  52. // otherwise the operations below will add mappings in an
  53. // arbitrary order.
  54. pm.mapMapping(srcs[0].Mapping[0])
  55. }
  56. for _, s := range src.Sample {
  57. if !isZeroSample(s) {
  58. pm.mapSample(s)
  59. }
  60. }
  61. }
  62. for _, s := range p.Sample {
  63. if isZeroSample(s) {
  64. // If there are any zero samples, re-merge the profile to GC
  65. // them.
  66. return Merge([]*Profile{p})
  67. }
  68. }
  69. return p, nil
  70. }
  71. func isZeroSample(s *Sample) bool {
  72. for _, v := range s.Value {
  73. if v != 0 {
  74. return false
  75. }
  76. }
  77. return true
  78. }
  79. type profileMerger struct {
  80. p *Profile
  81. // Memoization tables within a profile.
  82. locationsByID map[uint64]*Location
  83. functionsByID map[uint64]*Function
  84. mappingsByID map[uint64]mapInfo
  85. // Memoization tables for profile entities.
  86. samples map[string]*Sample
  87. locations map[string]*Location
  88. functions map[string]*Function
  89. mappings map[string]*Mapping
  90. }
  91. type mapInfo struct {
  92. m *Mapping
  93. offset int64
  94. }
  95. func (pm *profileMerger) mapSample(src *Sample) *Sample {
  96. s := &Sample{
  97. Location: make([]*Location, len(src.Location)),
  98. Value: make([]int64, len(src.Value)),
  99. Label: make(map[string][]string, len(src.Label)),
  100. NumLabel: make(map[string][]int64, len(src.NumLabel)),
  101. }
  102. for i, l := range src.Location {
  103. s.Location[i] = pm.mapLocation(l)
  104. }
  105. for k, v := range src.Label {
  106. vv := make([]string, len(v))
  107. copy(vv, v)
  108. s.Label[k] = vv
  109. }
  110. for k, v := range src.NumLabel {
  111. vv := make([]int64, len(v))
  112. copy(vv, v)
  113. s.NumLabel[k] = vv
  114. }
  115. // Check memoization table. Must be done on the remapped location to
  116. // account for the remapped mapping. Add current values to the
  117. // existing sample.
  118. k := s.key()
  119. if ss, ok := pm.samples[k]; ok {
  120. for i, v := range src.Value {
  121. ss.Value[i] += v
  122. }
  123. return ss
  124. }
  125. copy(s.Value, src.Value)
  126. pm.samples[k] = s
  127. pm.p.Sample = append(pm.p.Sample, s)
  128. return s
  129. }
  130. // key generates encoded string of Sample to be used as a key for maps.
  131. func (sample *Sample) key() (s string) {
  132. ids := make([]string, len(sample.Location))
  133. for i, l := range sample.Location {
  134. ids[i] = strconv.FormatUint(l.ID, 16)
  135. }
  136. s = strings.Join(ids, "|")
  137. if len(sample.Label) != 0 {
  138. labels := make([]string, 0, len(sample.Label))
  139. for k, v := range sample.Label {
  140. labels = append(labels, fmt.Sprintf("%q%q", k, v))
  141. }
  142. sort.Strings(labels)
  143. s += strings.Join(labels, "")
  144. }
  145. if len(sample.NumLabel) > 0 {
  146. labels := make([]string, 0, len(sample.NumLabel))
  147. for k, v := range sample.NumLabel {
  148. labels = append(labels, fmt.Sprintf("%q%v", k, v))
  149. }
  150. sort.Strings(labels)
  151. s += strings.Join(labels, "")
  152. }
  153. return s
  154. }
  155. func (pm *profileMerger) mapLocation(src *Location) *Location {
  156. if src == nil {
  157. return nil
  158. }
  159. if l, ok := pm.locationsByID[src.ID]; ok {
  160. pm.locationsByID[src.ID] = l
  161. return l
  162. }
  163. mi := pm.mapMapping(src.Mapping)
  164. l := &Location{
  165. ID: uint64(len(pm.p.Location) + 1),
  166. Mapping: mi.m,
  167. Address: uint64(int64(src.Address) + mi.offset),
  168. Line: make([]Line, len(src.Line)),
  169. }
  170. for i, ln := range src.Line {
  171. l.Line[i] = pm.mapLine(ln)
  172. }
  173. // Check memoization table. Must be done on the remapped location to
  174. // account for the remapped mapping ID.
  175. k := l.key()
  176. if ll, ok := pm.locations[k]; ok {
  177. pm.locationsByID[src.ID] = ll
  178. return ll
  179. }
  180. pm.locationsByID[src.ID] = l
  181. pm.locations[k] = l
  182. pm.p.Location = append(pm.p.Location, l)
  183. return l
  184. }
  185. // key generates encoded string of Location to be used as a key for maps.
  186. func (l *Location) key() string {
  187. addr := l.Address
  188. var s string
  189. if l.Mapping != nil {
  190. // Normalizes address to handle address space randomization.
  191. addr -= l.Mapping.Start
  192. s = strconv.FormatUint(l.Mapping.ID, 16) + "|" + strconv.FormatUint(addr, 16)
  193. } else {
  194. s = "0|" + strconv.FormatUint(addr, 16)
  195. }
  196. for _, line := range l.Line {
  197. s += strconv.FormatUint(line.Function.ID, 16) + "|" + strconv.FormatInt(line.Line, 16)
  198. }
  199. return s
  200. }
  201. func (pm *profileMerger) mapMapping(src *Mapping) mapInfo {
  202. if src == nil {
  203. return mapInfo{}
  204. }
  205. if mi, ok := pm.mappingsByID[src.ID]; ok {
  206. return mi
  207. }
  208. // Check memoization tables.
  209. bk, pk := src.key()
  210. if src.BuildID != "" {
  211. if m, ok := pm.mappings[bk]; ok {
  212. mi := mapInfo{m, int64(m.Start) - int64(src.Start)}
  213. pm.mappingsByID[src.ID] = mi
  214. return mi
  215. }
  216. }
  217. if src.File != "" {
  218. if m, ok := pm.mappings[pk]; ok {
  219. mi := mapInfo{m, int64(m.Start) - int64(src.Start)}
  220. pm.mappingsByID[src.ID] = mi
  221. return mi
  222. }
  223. }
  224. m := &Mapping{
  225. ID: uint64(len(pm.p.Mapping) + 1),
  226. Start: src.Start,
  227. Limit: src.Limit,
  228. Offset: src.Offset,
  229. File: src.File,
  230. BuildID: src.BuildID,
  231. HasFunctions: src.HasFunctions,
  232. HasFilenames: src.HasFilenames,
  233. HasLineNumbers: src.HasLineNumbers,
  234. HasInlineFrames: src.HasInlineFrames,
  235. }
  236. pm.p.Mapping = append(pm.p.Mapping, m)
  237. // Update memoization tables.
  238. if m.BuildID != "" {
  239. pm.mappings[bk] = m
  240. }
  241. if m.File != "" {
  242. pm.mappings[pk] = m
  243. }
  244. mi := mapInfo{m, 0}
  245. pm.mappingsByID[src.ID] = mi
  246. return mi
  247. }
  248. // key generates encoded strings of Mapping to be used as a key for
  249. // maps. The first key represents only the build id, while the second
  250. // represents only the file path.
  251. func (m *Mapping) key() (buildIDKey, pathKey string) {
  252. // Normalize addresses to handle address space randomization.
  253. // Round up to next 4K boundary to avoid minor discrepancies.
  254. const mapsizeRounding = 0x1000
  255. size := m.Limit - m.Start
  256. size = size + mapsizeRounding - 1
  257. size = size - (size % mapsizeRounding)
  258. key := strconv.FormatUint(size, 16) + "|" + strconv.FormatUint(m.Offset, 16)
  259. buildIDKey = key + "B" + strconv.Quote(m.BuildID)
  260. pathKey = key + "F" + strconv.Quote(m.File)
  261. return
  262. }
  263. func (pm *profileMerger) mapLine(src Line) Line {
  264. ln := Line{
  265. Function: pm.mapFunction(src.Function),
  266. Line: src.Line,
  267. }
  268. return ln
  269. }
  270. func (pm *profileMerger) mapFunction(src *Function) *Function {
  271. if src == nil {
  272. return nil
  273. }
  274. if f, ok := pm.functionsByID[src.ID]; ok {
  275. return f
  276. }
  277. k := src.key()
  278. if f, ok := pm.functions[k]; ok {
  279. pm.functionsByID[src.ID] = f
  280. return f
  281. }
  282. f := &Function{
  283. ID: uint64(len(pm.p.Function) + 1),
  284. Name: src.Name,
  285. SystemName: src.SystemName,
  286. Filename: src.Filename,
  287. StartLine: src.StartLine,
  288. }
  289. pm.functions[k] = f
  290. pm.functionsByID[src.ID] = f
  291. pm.p.Function = append(pm.p.Function, f)
  292. return f
  293. }
  294. // key generates encoded string of Function to be used as a key for maps.
  295. func (f *Function) key() string {
  296. return fmt.Sprintf("%x%q%q%q", f.StartLine, f.Name, f.SystemName, f.Filename)
  297. }
  298. // combineHeaders checks that all profiles can be merged and returns
  299. // their combined profile.
  300. func combineHeaders(srcs []*Profile) (*Profile, error) {
  301. for _, s := range srcs[1:] {
  302. if err := srcs[0].compatible(s); err != nil {
  303. return nil, err
  304. }
  305. }
  306. var timeNanos, durationNanos, period int64
  307. var comments []string
  308. for _, s := range srcs {
  309. if timeNanos == 0 || s.TimeNanos < timeNanos {
  310. timeNanos = s.TimeNanos
  311. }
  312. durationNanos += s.DurationNanos
  313. if period == 0 || period < s.Period {
  314. period = s.Period
  315. }
  316. comments = append(comments, s.Comments...)
  317. }
  318. p := &Profile{
  319. SampleType: make([]*ValueType, len(srcs[0].SampleType)),
  320. DropFrames: srcs[0].DropFrames,
  321. KeepFrames: srcs[0].KeepFrames,
  322. TimeNanos: timeNanos,
  323. DurationNanos: durationNanos,
  324. PeriodType: srcs[0].PeriodType,
  325. Period: period,
  326. Comments: comments,
  327. }
  328. copy(p.SampleType, srcs[0].SampleType)
  329. return p, nil
  330. }
  331. // compatible determines if two profiles can be compared/merged.
  332. // returns nil if the profiles are compatible; otherwise an error with
  333. // details on the incompatibility.
  334. func (p *Profile) compatible(pb *Profile) error {
  335. if !reflect.DeepEqual(p.PeriodType, pb.PeriodType) {
  336. return fmt.Errorf("incompatible period types %v and %v", p.PeriodType, pb.PeriodType)
  337. }
  338. if len(p.SampleType) != len(pb.SampleType) {
  339. return fmt.Errorf("incompatible sample types %v and %v", p.SampleType, pb.SampleType)
  340. }
  341. for i := range p.SampleType {
  342. if !reflect.DeepEqual(p.SampleType[i], pb.SampleType[i]) {
  343. return fmt.Errorf("incompatible sample types %v and %v", p.SampleType, pb.SampleType)
  344. }
  345. }
  346. return nil
  347. }