Brak opisu

merge.go 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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(srcs[0].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. }
  138. for i, l := range src.Location {
  139. s.Location[i] = pm.mapLocation(l)
  140. }
  141. for k, v := range src.Label {
  142. vv := make([]string, len(v))
  143. copy(vv, v)
  144. s.Label[k] = vv
  145. }
  146. for k, v := range src.NumLabel {
  147. vv := make([]int64, len(v))
  148. copy(vv, v)
  149. s.NumLabel[k] = vv
  150. }
  151. // Check memoization table. Must be done on the remapped location to
  152. // account for the remapped mapping. Add current values to the
  153. // existing sample.
  154. k := s.key()
  155. if ss, ok := pm.samples[k]; ok {
  156. for i, v := range src.Value {
  157. ss.Value[i] += v
  158. }
  159. return ss
  160. }
  161. copy(s.Value, src.Value)
  162. pm.samples[k] = s
  163. pm.p.Sample = append(pm.p.Sample, s)
  164. return s
  165. }
  166. // key generates sampleKey to be used as a key for maps.
  167. func (sample *Sample) key() sampleKey {
  168. ids := make([]string, len(sample.Location))
  169. for i, l := range sample.Location {
  170. ids[i] = strconv.FormatUint(l.ID, 16)
  171. }
  172. labels := make([]string, 0, len(sample.Label))
  173. for k, v := range sample.Label {
  174. labels = append(labels, fmt.Sprintf("%q%q", k, v))
  175. }
  176. sort.Strings(labels)
  177. numlabels := make([]string, 0, len(sample.NumLabel))
  178. for k, v := range sample.NumLabel {
  179. numlabels = append(numlabels, fmt.Sprintf("%q%x", k, v))
  180. }
  181. sort.Strings(numlabels)
  182. return sampleKey{
  183. strings.Join(ids, "|"),
  184. strings.Join(labels, ""),
  185. strings.Join(numlabels, ""),
  186. }
  187. }
  188. type sampleKey struct {
  189. locations string
  190. labels string
  191. numlabels string
  192. }
  193. func (pm *profileMerger) mapLocation(src *Location) *Location {
  194. if src == nil {
  195. return nil
  196. }
  197. if l, ok := pm.locationsByID[src.ID]; ok {
  198. pm.locationsByID[src.ID] = l
  199. return l
  200. }
  201. mi := pm.mapMapping(src.Mapping)
  202. l := &Location{
  203. ID: uint64(len(pm.p.Location) + 1),
  204. Mapping: mi.m,
  205. Address: uint64(int64(src.Address) + mi.offset),
  206. Line: make([]Line, len(src.Line)),
  207. }
  208. for i, ln := range src.Line {
  209. l.Line[i] = pm.mapLine(ln)
  210. }
  211. // Check memoization table. Must be done on the remapped location to
  212. // account for the remapped mapping ID.
  213. k := l.key()
  214. if ll, ok := pm.locations[k]; ok {
  215. pm.locationsByID[src.ID] = ll
  216. return ll
  217. }
  218. pm.locationsByID[src.ID] = l
  219. pm.locations[k] = l
  220. pm.p.Location = append(pm.p.Location, l)
  221. return l
  222. }
  223. // key generates locationKey to be used as a key for maps.
  224. func (l *Location) key() locationKey {
  225. key := locationKey{
  226. addr: l.Address,
  227. }
  228. if l.Mapping != nil {
  229. // Normalizes address to handle address space randomization.
  230. key.addr -= l.Mapping.Start
  231. key.mappingID = l.Mapping.ID
  232. }
  233. lines := make([]string, len(l.Line)*2)
  234. for i, line := range l.Line {
  235. if line.Function != nil {
  236. lines[i*2] = strconv.FormatUint(line.Function.ID, 16)
  237. }
  238. lines[i*2+1] = strconv.FormatInt(line.Line, 16)
  239. }
  240. key.lines = strings.Join(lines, "|")
  241. return key
  242. }
  243. type locationKey struct {
  244. addr, mappingID uint64
  245. lines string
  246. }
  247. func (pm *profileMerger) mapMapping(src *Mapping) mapInfo {
  248. if src == nil {
  249. return mapInfo{}
  250. }
  251. if mi, ok := pm.mappingsByID[src.ID]; ok {
  252. return mi
  253. }
  254. // Check memoization tables.
  255. bk, pk := src.key()
  256. if src.BuildID != "" {
  257. if m, ok := pm.mappings[bk]; ok {
  258. mi := mapInfo{m, int64(m.Start) - int64(src.Start)}
  259. pm.mappingsByID[src.ID] = mi
  260. return mi
  261. }
  262. }
  263. if src.File != "" {
  264. if m, ok := pm.mappings[pk]; ok {
  265. mi := mapInfo{m, int64(m.Start) - int64(src.Start)}
  266. pm.mappingsByID[src.ID] = mi
  267. return mi
  268. }
  269. }
  270. m := &Mapping{
  271. ID: uint64(len(pm.p.Mapping) + 1),
  272. Start: src.Start,
  273. Limit: src.Limit,
  274. Offset: src.Offset,
  275. File: src.File,
  276. BuildID: src.BuildID,
  277. HasFunctions: src.HasFunctions,
  278. HasFilenames: src.HasFilenames,
  279. HasLineNumbers: src.HasLineNumbers,
  280. HasInlineFrames: src.HasInlineFrames,
  281. }
  282. pm.p.Mapping = append(pm.p.Mapping, m)
  283. // Update memoization tables.
  284. if m.BuildID != "" {
  285. pm.mappings[bk] = m
  286. }
  287. if m.File != "" {
  288. pm.mappings[pk] = m
  289. }
  290. mi := mapInfo{m, 0}
  291. pm.mappingsByID[src.ID] = mi
  292. return mi
  293. }
  294. // key generates encoded strings of Mapping to be used as a key for
  295. // maps. The first key represents only the build id, while the second
  296. // represents only the file path.
  297. func (m *Mapping) key() (buildIDKey, pathKey mappingKey) {
  298. // Normalize addresses to handle address space randomization.
  299. // Round up to next 4K boundary to avoid minor discrepancies.
  300. const mapsizeRounding = 0x1000
  301. size := m.Limit - m.Start
  302. size = size + mapsizeRounding - 1
  303. size = size - (size % mapsizeRounding)
  304. buildIDKey = mappingKey{
  305. size,
  306. m.Offset,
  307. m.BuildID,
  308. }
  309. pathKey = mappingKey{
  310. size,
  311. m.Offset,
  312. m.File,
  313. }
  314. return
  315. }
  316. type mappingKey struct {
  317. size, offset uint64
  318. buildidIDOrFile string
  319. }
  320. func (pm *profileMerger) mapLine(src Line) Line {
  321. ln := Line{
  322. Function: pm.mapFunction(src.Function),
  323. Line: src.Line,
  324. }
  325. return ln
  326. }
  327. func (pm *profileMerger) mapFunction(src *Function) *Function {
  328. if src == nil {
  329. return nil
  330. }
  331. if f, ok := pm.functionsByID[src.ID]; ok {
  332. return f
  333. }
  334. k := src.key()
  335. if f, ok := pm.functions[k]; ok {
  336. pm.functionsByID[src.ID] = f
  337. return f
  338. }
  339. f := &Function{
  340. ID: uint64(len(pm.p.Function) + 1),
  341. Name: src.Name,
  342. SystemName: src.SystemName,
  343. Filename: src.Filename,
  344. StartLine: src.StartLine,
  345. }
  346. pm.functions[k] = f
  347. pm.functionsByID[src.ID] = f
  348. pm.p.Function = append(pm.p.Function, f)
  349. return f
  350. }
  351. // key generates a struct to be used as a key for maps.
  352. func (f *Function) key() functionKey {
  353. return functionKey{
  354. f.StartLine,
  355. f.Name,
  356. f.SystemName,
  357. f.Filename,
  358. }
  359. }
  360. type functionKey struct {
  361. startLine int64
  362. name, systemName, fileName string
  363. }
  364. // combineHeaders checks that all profiles can be merged and returns
  365. // their combined profile.
  366. func combineHeaders(srcs []*Profile) (*Profile, error) {
  367. for _, s := range srcs[1:] {
  368. if err := srcs[0].compatible(s); err != nil {
  369. return nil, err
  370. }
  371. }
  372. var timeNanos, durationNanos, period int64
  373. var comments []string
  374. var defaultSampleType string
  375. for _, s := range srcs {
  376. if timeNanos == 0 || s.TimeNanos < timeNanos {
  377. timeNanos = s.TimeNanos
  378. }
  379. durationNanos += s.DurationNanos
  380. if period == 0 || period < s.Period {
  381. period = s.Period
  382. }
  383. comments = append(comments, s.Comments...)
  384. if defaultSampleType == "" {
  385. defaultSampleType = s.DefaultSampleType
  386. }
  387. }
  388. p := &Profile{
  389. SampleType: make([]*ValueType, len(srcs[0].SampleType)),
  390. DropFrames: srcs[0].DropFrames,
  391. KeepFrames: srcs[0].KeepFrames,
  392. TimeNanos: timeNanos,
  393. DurationNanos: durationNanos,
  394. PeriodType: srcs[0].PeriodType,
  395. Period: period,
  396. Comments: comments,
  397. DefaultSampleType: defaultSampleType,
  398. }
  399. copy(p.SampleType, srcs[0].SampleType)
  400. return p, nil
  401. }
  402. // compatible determines if two profiles can be compared/merged.
  403. // returns nil if the profiles are compatible; otherwise an error with
  404. // details on the incompatibility.
  405. func (p *Profile) compatible(pb *Profile) error {
  406. if !equalValueType(p.PeriodType, pb.PeriodType) {
  407. return fmt.Errorf("incompatible period types %v and %v", p.PeriodType, pb.PeriodType)
  408. }
  409. if len(p.SampleType) != len(pb.SampleType) {
  410. return fmt.Errorf("incompatible sample types %v and %v", p.SampleType, pb.SampleType)
  411. }
  412. for i := range p.SampleType {
  413. if !equalValueType(p.SampleType[i], pb.SampleType[i]) {
  414. return fmt.Errorf("incompatible sample types %v and %v", p.SampleType, pb.SampleType)
  415. }
  416. }
  417. return nil
  418. }
  419. // equalValueType returns true if the two value types are semantically
  420. // equal. It ignores the internal fields used during encode/decode.
  421. func equalValueType(st1, st2 *ValueType) bool {
  422. return st1.Type == st2.Type && st1.Unit == st2.Unit
  423. }