1 /*
2  * Copyright (C) 2016 The Android Open Source Project
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  */
16 
17 #ifndef ART_PROFMAN_PROFILE_ASSISTANT_H_
18 #define ART_PROFMAN_PROFILE_ASSISTANT_H_
19 
20 #include <string>
21 #include <vector>
22 
23 #include "base/scoped_flock.h"
24 #include "profile/profile_compilation_info.h"
25 
26 namespace art {
27 
28 class ProfileAssistant {
29  public:
30   // These also serve as return codes of profman and are processed by installd
31   // (frameworks/native/cmds/installd/commands.cpp)
32   enum ProcessingResult {
33     kSuccess = 0,  // Generic success code for non-analysis runs.
34     kCompile = 1,
35     kSkipCompilation = 2,
36     kErrorBadProfiles = 3,
37     kErrorIO = 4,
38     kErrorCannotLock = 5,
39     kErrorDifferentVersions = 6,
40   };
41 
42   class Options {
43    public:
44     static constexpr bool kForceMergeDefault = false;
45     static constexpr bool kBootImageMergeDefault = false;
46 
Options()47     Options()
48         : force_merge_(kForceMergeDefault),
49           boot_image_merge_(kBootImageMergeDefault) {
50     }
51 
IsForceMerge()52     bool IsForceMerge() const { return force_merge_; }
IsBootImageMerge()53     bool IsBootImageMerge() const { return boot_image_merge_; }
54 
SetForceMerge(bool value)55     void SetForceMerge(bool value) { force_merge_ = value; }
SetBootImageMerge(bool value)56     void SetBootImageMerge(bool value) { boot_image_merge_ = value; }
57 
58    private:
59     // If true, performs a forced merge, without analyzing if there is a
60     // significant difference between the current profile and the reference profile.
61     // See ProfileAssistant#ProcessProfile.
62     bool force_merge_;
63     // Signals that the merge is for boot image profiles. It will ignore differences
64     // in profile versions (instead of aborting).
65     bool boot_image_merge_;
66   };
67 
68   // Process the profile information present in the given files. Returns one of
69   // ProcessingResult values depending on profile information and whether or not
70   // the analysis ended up successfully (i.e. no errors during reading,
71   // merging or writing of profile files).
72   //
73   // When the returned value is kCompile there is a significant difference
74   // between profile_files and reference_profile_files. In this case
75   // reference_profile will be updated with the profiling info obtain after
76   // merging all profiles.
77   //
78   // When the returned value is kSkipCompilation, the difference between the
79   // merge of the current profiles and the reference one is insignificant. In
80   // this case no file will be updated.
81   //
82   static ProcessingResult ProcessProfiles(
83       const std::vector<std::string>& profile_files,
84       const std::string& reference_profile_file,
85       const ProfileCompilationInfo::ProfileLoadFilterFn& filter_fn
86           = ProfileCompilationInfo::ProfileFilterFnAcceptAll,
87       const Options& options = Options());
88 
89   static ProcessingResult ProcessProfiles(
90       const std::vector<int>& profile_files_fd_,
91       int reference_profile_file_fd,
92       const ProfileCompilationInfo::ProfileLoadFilterFn& filter_fn
93           = ProfileCompilationInfo::ProfileFilterFnAcceptAll,
94       const Options& options = Options());
95 
96  private:
97   static ProcessingResult ProcessProfilesInternal(
98       const std::vector<ScopedFlock>& profile_files,
99       const ScopedFlock& reference_profile_file,
100       const ProfileCompilationInfo::ProfileLoadFilterFn& filter_fn,
101       const Options& options);
102 
103   DISALLOW_COPY_AND_ASSIGN(ProfileAssistant);
104 };
105 
106 }  // namespace art
107 
108 #endif  // ART_PROFMAN_PROFILE_ASSISTANT_H_
109