You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

480 lines
12 KiB

  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. "sort"
  18. "strconv"
  19. "strings"
  20. )
  21. // Compact performs garbage collection on a profile to remove any
  22. // unreferenced fields. This is useful to reduce the size of a profile
  23. // after samples or locations have been removed.
  24. func (p *Profile) Compact() *Profile {
  25. p, _ = Merge([]*Profile{p})
  26. return p
  27. }
  28. // Merge merges all the profiles in profs into a single Profile.
  29. // Returns a new profile independent of the input profiles. The merged
  30. // profile is compacted to eliminate unused samples, locations,
  31. // functions and mappings. Profiles must have identical profile sample
  32. // and period types or the merge will fail. profile.Period of the
  33. // resulting profile will be the maximum of all profiles, and
  34. // profile.TimeNanos will be the earliest nonzero one.
  35. func Merge(srcs []*Profile) (*Profile, error) {
  36. if len(srcs) == 0 {
  37. return nil, fmt.Errorf("no profiles to merge")
  38. }
  39. p, err := combineHeaders(srcs)
  40. if err != nil {
  41. return nil, err
  42. }
  43. pm := &profileMerger{
  44. p: p,
  45. samples: make(map[sampleKey]*Sample, len(srcs[0].Sample)),
  46. locations: make(map[locationKey]*Location, len(srcs[0].Location)),
  47. functions: make(map[functionKey]*Function, len(srcs[0].Function)),
  48. mappings: make(map[mappingKey]*Mapping, len(srcs[0].Mapping)),
  49. }
  50. for _, src := range srcs {
  51. // Clear the profile-specific hash tables
  52. pm.locationsByID = make(map[uint64]*Location, len(src.Location))
  53. pm.functionsByID = make(map[uint64]*Function, len(src.Function))
  54. pm.mappingsByID = make(map[uint64]mapInfo, len(src.Mapping))
  55. if len(pm.mappings) == 0 && len(src.Mapping) > 0 {
  56. // The Mapping list has the property that the first mapping
  57. // represents the main binary. Take the first Mapping we see,
  58. // otherwise the operations below will add mappings in an
  59. // arbitrary order.
  60. pm.mapMapping(src.Mapping[0])
  61. }
  62. for _, s := range src.Sample {
  63. if !isZeroSample(s) {
  64. pm.mapSample(s)
  65. }
  66. }
  67. }
  68. for _, s := range p.Sample {
  69. if isZeroSample(s) {
  70. // If there are any zero samples, re-merge the profile to GC
  71. // them.
  72. return Merge([]*Profile{p})
  73. }
  74. }
  75. return p, nil
  76. }
  77. // Normalize normalizes the source profile by multiplying each value in profile by the
  78. // ratio of the sum of the base profile's values of that sample type to the sum of the
  79. // source profile's value of that sample type.
  80. func (p *Profile) Normalize(pb *Profile) error {
  81. if err := p.compatible(pb); err != nil {
  82. return err
  83. }
  84. baseVals := make([]int64, len(p.SampleType))
  85. for _, s := range pb.Sample {
  86. for i, v := range s.Value {
  87. baseVals[i] += v
  88. }
  89. }
  90. srcVals := make([]int64, len(p.SampleType))
  91. for _, s := range p.Sample {
  92. for i, v := range s.Value {
  93. srcVals[i] += v
  94. }
  95. }
  96. normScale := make([]float64, len(baseVals))
  97. for i := range baseVals {
  98. if srcVals[i] == 0 {
  99. normScale[i] = 0.0
  100. } else {
  101. normScale[i] = float64(baseVals[i]) / float64(srcVals[i])
  102. }
  103. }
  104. p.ScaleN(normScale)
  105. return nil
  106. }
  107. func isZeroSample(s *Sample) bool {
  108. for _, v := range s.Value {
  109. if v != 0 {
  110. return false
  111. }
  112. }
  113. return true
  114. }
  115. type profileMerger struct {
  116. p *Profile
  117. // Memoization tables within a profile.
  118. locationsByID map[uint64]*Location
  119. functionsByID map[uint64]*Function
  120. mappingsByID map[uint64]mapInfo
  121. // Memoization tables for profile entities.
  122. samples map[sampleKey]*Sample
  123. locations map[locationKey]*Location
  124. functions map[functionKey]*Function
  125. mappings map[mappingKey]*Mapping
  126. }
  127. type mapInfo struct {
  128. m *Mapping
  129. offset int64
  130. }
  131. func (pm *profileMerger) mapSample(src *Sample) *Sample {
  132. s := &Sample{
  133. Location: make([]*Location, len(src.Location)),
  134. Value: make([]int64, len(src.Value)),
  135. Label: make(map[string][]string, len(src.Label)),
  136. NumLabel: make(map[string][]int64, len(src.NumLabel)),
  137. NumUnit: make(map[string][]string, len(src.NumLabel)),
  138. }
  139. for i, l := range src.Location {
  140. s.Location[i] = pm.mapLocation(l)
  141. }
  142. for k, v := range src.Label {
  143. vv := make([]string, len(v))
  144. copy(vv, v)
  145. s.Label[k] = vv
  146. }
  147. for k, v := range src.NumLabel {
  148. u := src.NumUnit[k]
  149. vv := make([]int64, len(v))
  150. uu := make([]string, len(u))
  151. copy(vv, v)
  152. copy(uu, u)
  153. s.NumLabel[k] = vv
  154. s.NumUnit[k] = uu
  155. }
  156. // Check memoization table. Must be done on the remapped location to
  157. // account for the remapped mapping. Add current values to the
  158. // existing sample.
  159. k := s.key()
  160. if ss, ok := pm.samples[k]; ok {
  161. for i, v := range src.Value {
  162. ss.Value[i] += v
  163. }
  164. return ss
  165. }
  166. copy(s.Value, src.Value)
  167. pm.samples[k] = s
  168. pm.p.Sample = append(pm.p.Sample, s)
  169. return s
  170. }
  171. // key generates sampleKey to be used as a key for maps.
  172. func (sample *Sample) key() sampleKey {
  173. ids := make([]string, len(sample.Location))
  174. for i, l := range sample.Location {
  175. ids[i] = strconv.FormatUint(l.ID, 16)
  176. }
  177. labels := make([]string, 0, len(sample.Label))
  178. for k, v := range sample.Label {
  179. labels = append(labels, fmt.Sprintf("%q%q", k, v))
  180. }
  181. sort.Strings(labels)
  182. numlabels := make([]string, 0, len(sample.NumLabel))
  183. for k, v := range sample.NumLabel {
  184. numlabels = append(numlabels, fmt.Sprintf("%q%x%x", k, v, sample.NumUnit[k]))
  185. }
  186. sort.Strings(numlabels)
  187. return sampleKey{
  188. strings.Join(ids, "|"),
  189. strings.Join(labels, ""),
  190. strings.Join(numlabels, ""),
  191. }
  192. }
  193. type sampleKey struct {
  194. locations string
  195. labels string
  196. numlabels string
  197. }
  198. func (pm *profileMerger) mapLocation(src *Location) *Location {
  199. if src == nil {
  200. return nil
  201. }
  202. if l, ok := pm.locationsByID[src.ID]; ok {
  203. pm.locationsByID[src.ID] = l
  204. return l
  205. }
  206. mi := pm.mapMapping(src.Mapping)
  207. l := &Location{
  208. ID: uint64(len(pm.p.Location) + 1),
  209. Mapping: mi.m,
  210. Address: uint64(int64(src.Address) + mi.offset),
  211. Line: make([]Line, len(src.Line)),
  212. IsFolded: src.IsFolded,
  213. }
  214. for i, ln := range src.Line {
  215. l.Line[i] = pm.mapLine(ln)
  216. }
  217. // Check memoization table. Must be done on the remapped location to
  218. // account for the remapped mapping ID.
  219. k := l.key()
  220. if ll, ok := pm.locations[k]; ok {
  221. pm.locationsByID[src.ID] = ll
  222. return ll
  223. }
  224. pm.locationsByID[src.ID] = l
  225. pm.locations[k] = l
  226. pm.p.Location = append(pm.p.Location, l)
  227. return l
  228. }
  229. // key generates locationKey to be used as a key for maps.
  230. func (l *Location) key() locationKey {
  231. key := locationKey{
  232. addr: l.Address,
  233. isFolded: l.IsFolded,
  234. }
  235. if l.Mapping != nil {
  236. // Normalizes address to handle address space randomization.
  237. key.addr -= l.Mapping.Start
  238. key.mappingID = l.Mapping.ID
  239. }
  240. lines := make([]string, len(l.Line)*2)
  241. for i, line := range l.Line {
  242. if line.Function != nil {
  243. lines[i*2] = strconv.FormatUint(line.Function.ID, 16)
  244. }
  245. lines[i*2+1] = strconv.FormatInt(line.Line, 16)
  246. }
  247. key.lines = strings.Join(lines, "|")
  248. return key
  249. }
  250. type locationKey struct {
  251. addr, mappingID uint64
  252. lines string
  253. isFolded bool
  254. }
  255. func (pm *profileMerger) mapMapping(src *Mapping) mapInfo {
  256. if src == nil {
  257. return mapInfo{}
  258. }
  259. if mi, ok := pm.mappingsByID[src.ID]; ok {
  260. return mi
  261. }
  262. // Check memoization tables.
  263. mk := src.key()
  264. if m, ok := pm.mappings[mk]; ok {
  265. mi := mapInfo{m, int64(m.Start) - int64(src.Start)}
  266. pm.mappingsByID[src.ID] = mi
  267. return mi
  268. }
  269. m := &Mapping{
  270. ID: uint64(len(pm.p.Mapping) + 1),
  271. Start: src.Start,
  272. Limit: src.Limit,
  273. Offset: src.Offset,
  274. File: src.File,
  275. BuildID: src.BuildID,
  276. HasFunctions: src.HasFunctions,
  277. HasFilenames: src.HasFilenames,
  278. HasLineNumbers: src.HasLineNumbers,
  279. HasInlineFrames: src.HasInlineFrames,
  280. }
  281. pm.p.Mapping = append(pm.p.Mapping, m)
  282. // Update memoization tables.
  283. pm.mappings[mk] = m
  284. mi := mapInfo{m, 0}
  285. pm.mappingsByID[src.ID] = mi
  286. return mi
  287. }
  288. // key generates encoded strings of Mapping to be used as a key for
  289. // maps.
  290. func (m *Mapping) key() mappingKey {
  291. // Normalize addresses to handle address space randomization.
  292. // Round up to next 4K boundary to avoid minor discrepancies.
  293. const mapsizeRounding = 0x1000
  294. size := m.Limit - m.Start
  295. size = size + mapsizeRounding - 1
  296. size = size - (size % mapsizeRounding)
  297. key := mappingKey{
  298. size: size,
  299. offset: m.Offset,
  300. }
  301. switch {
  302. case m.BuildID != "":
  303. key.buildIDOrFile = m.BuildID
  304. case m.File != "":
  305. key.buildIDOrFile = m.File
  306. default:
  307. // A mapping containing neither build ID nor file name is a fake mapping. A
  308. // key with empty buildIDOrFile is used for fake mappings so that they are
  309. // treated as the same mapping during merging.
  310. }
  311. return key
  312. }
  313. type mappingKey struct {
  314. size, offset uint64
  315. buildIDOrFile string
  316. }
  317. func (pm *profileMerger) mapLine(src Line) Line {
  318. ln := Line{
  319. Function: pm.mapFunction(src.Function),
  320. Line: src.Line,
  321. }
  322. return ln
  323. }
  324. func (pm *profileMerger) mapFunction(src *Function) *Function {
  325. if src == nil {
  326. return nil
  327. }
  328. if f, ok := pm.functionsByID[src.ID]; ok {
  329. return f
  330. }
  331. k := src.key()
  332. if f, ok := pm.functions[k]; ok {
  333. pm.functionsByID[src.ID] = f
  334. return f
  335. }
  336. f := &Function{
  337. ID: uint64(len(pm.p.Function) + 1),
  338. Name: src.Name,
  339. SystemName: src.SystemName,
  340. Filename: src.Filename,
  341. StartLine: src.StartLine,
  342. }
  343. pm.functions[k] = f
  344. pm.functionsByID[src.ID] = f
  345. pm.p.Function = append(pm.p.Function, f)
  346. return f
  347. }
  348. // key generates a struct to be used as a key for maps.
  349. func (f *Function) key() functionKey {
  350. return functionKey{
  351. f.StartLine,
  352. f.Name,
  353. f.SystemName,
  354. f.Filename,
  355. }
  356. }
  357. type functionKey struct {
  358. startLine int64
  359. name, systemName, fileName string
  360. }
  361. // combineHeaders checks that all profiles can be merged and returns
  362. // their combined profile.
  363. func combineHeaders(srcs []*Profile) (*Profile, error) {
  364. for _, s := range srcs[1:] {
  365. if err := srcs[0].compatible(s); err != nil {
  366. return nil, err
  367. }
  368. }
  369. var timeNanos, durationNanos, period int64
  370. var comments []string
  371. seenComments := map[string]bool{}
  372. var defaultSampleType string
  373. for _, s := range srcs {
  374. if timeNanos == 0 || s.TimeNanos < timeNanos {
  375. timeNanos = s.TimeNanos
  376. }
  377. durationNanos += s.DurationNanos
  378. if period == 0 || period < s.Period {
  379. period = s.Period
  380. }
  381. for _, c := range s.Comments {
  382. if seen := seenComments[c]; !seen {
  383. comments = append(comments, c)
  384. seenComments[c] = true
  385. }
  386. }
  387. if defaultSampleType == "" {
  388. defaultSampleType = s.DefaultSampleType
  389. }
  390. }
  391. p := &Profile{
  392. SampleType: make([]*ValueType, len(srcs[0].SampleType)),
  393. DropFrames: srcs[0].DropFrames,
  394. KeepFrames: srcs[0].KeepFrames,
  395. TimeNanos: timeNanos,
  396. DurationNanos: durationNanos,
  397. PeriodType: srcs[0].PeriodType,
  398. Period: period,
  399. Comments: comments,
  400. DefaultSampleType: defaultSampleType,
  401. }
  402. copy(p.SampleType, srcs[0].SampleType)
  403. return p, nil
  404. }
  405. // compatible determines if two profiles can be compared/merged.
  406. // returns nil if the profiles are compatible; otherwise an error with
  407. // details on the incompatibility.
  408. func (p *Profile) compatible(pb *Profile) error {
  409. if !equalValueType(p.PeriodType, pb.PeriodType) {
  410. return fmt.Errorf("incompatible period types %v and %v", p.PeriodType, pb.PeriodType)
  411. }
  412. if len(p.SampleType) != len(pb.SampleType) {
  413. return fmt.Errorf("incompatible sample types %v and %v", p.SampleType, pb.SampleType)
  414. }
  415. for i := range p.SampleType {
  416. if !equalValueType(p.SampleType[i], pb.SampleType[i]) {
  417. return fmt.Errorf("incompatible sample types %v and %v", p.SampleType, pb.SampleType)
  418. }
  419. }
  420. return nil
  421. }
  422. // equalValueType returns true if the two value types are semantically
  423. // equal. It ignores the internal fields used during encode/decode.
  424. func equalValueType(st1, st2 *ValueType) bool {
  425. return st1.Type == st2.Type && st1.Unit == st2.Unit
  426. }