No Description

merge.go 11KB

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