Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e59f59ba authored by Treehugger Robot's avatar Treehugger Robot Committed by Automerger Merge Worker
Browse files

Merge "resourcemanager: move utility functions" into main am: 7d42ffbb am: 9c2c0d5b

parents 3af79ea4 9c2c0d5b
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -77,6 +77,7 @@ cc_library {
        "ResourceManagerMetrics.cpp",
        "ResourceManagerService.cpp",
        "ResourceObserverService.cpp",
        "ResourceManagerServiceUtils.cpp",
        "ServiceLog.cpp",
        "UidObserver.cpp",

+2 −83
Original line number Diff line number Diff line
@@ -39,6 +39,7 @@
#include "IMediaResourceMonitor.h"
#include "ResourceManagerMetrics.h"
#include "ResourceManagerService.h"
#include "ResourceManagerServiceUtils.h"
#include "ResourceObserverService.h"
#include "ServiceLog.h"

@@ -160,87 +161,6 @@ void OverrideProcessInfoDeathNotifier::binderDied() {
    service->removeProcessInfoOverride(mClientInfo.pid);
}

template <typename T>
static String8 getString(const std::vector<T>& items) {
    String8 itemsStr;
    for (size_t i = 0; i < items.size(); ++i) {
        itemsStr.appendFormat("%s ", toString(items[i]).c_str());
    }
    return itemsStr;
}

static bool hasResourceType(MediaResource::Type type, MediaResource::SubType subType,
        const MediaResourceParcel& resource) {
    if (type != resource.type) {
      return false;
    }
    switch (type) {
        // Codec subtypes (e.g. video vs. audio) are each considered separate resources, so
        // compare the subtypes as well.
        case MediaResource::Type::kSecureCodec:
        case MediaResource::Type::kNonSecureCodec:
            if (resource.subType == subType) {
                return true;
            }
            break;
        // Non-codec resources are not segregated by the subtype (e.g. video vs. audio).
        default:
            return true;
    }
    return false;
}

static bool hasResourceType(MediaResource::Type type, MediaResource::SubType subType,
        const ResourceList& resources) {
    for (auto it = resources.begin(); it != resources.end(); it++) {
        if (hasResourceType(type, subType, it->second)) {
            return true;
        }
    }
    return false;
}

static bool hasResourceType(MediaResource::Type type, MediaResource::SubType subType,
        const ResourceInfos& infos) {
    for (const auto& [id, info] : infos) {
        if (hasResourceType(type, subType, info.resources)) {
            return true;
        }
    }
    return false;
}

static ResourceInfos& getResourceInfosForEdit(int pid, PidResourceInfosMap& map) {
    PidResourceInfosMap::iterator found = map.find(pid);
    if (found == map.end()) {
        // new pid
        ResourceInfos infosForPid;
        auto [it, inserted] = map.emplace(pid, infosForPid);
        found = it;
    }

    return found->second;
}

static ResourceInfo& getResourceInfoForEdit(uid_t uid, int64_t clientId,
                                            const std::string& name,
        const std::shared_ptr<IResourceManagerClient>& client, ResourceInfos& infos) {
    ResourceInfos::iterator found = infos.find(clientId);

    if (found == infos.end()) {
        ResourceInfo info{.uid = uid,
                          .clientId = clientId,
                          .name = name.empty()? "<unknown client>" : name,
                          .client = client,
                          .deathNotifier = nullptr,
                          .pendingRemoval = false};
        auto [it, inserted] = infos.emplace(clientId, info);
        found = it;
    }

    return found->second;
}

static void notifyResourceGranted(int pid, const std::vector<MediaResourceParcel>& resources) {
    static const char* const kServiceName = "media_resource_monitor";
    sp<IBinder> binder = defaultServiceManager()->checkService(String16(kServiceName));
@@ -488,7 +408,6 @@ Status ResourceManagerService::addResource(const ClientInfoParcel& clientInfo,
    int32_t pid = clientInfo.pid;
    int32_t uid = clientInfo.uid;
    int64_t clientId = clientInfo.id;
    const std::string& name = clientInfo.name;
    String8 log = String8::format("addResource(pid %d, uid %d clientId %lld, resources %s)",
            pid, uid, (long long) clientId, getString(resources).c_str());
    mServiceLog->add(log);
@@ -503,7 +422,7 @@ Status ResourceManagerService::addResource(const ClientInfoParcel& clientInfo,
        uid = callingUid;
    }
    ResourceInfos& infos = getResourceInfosForEdit(pid, mMap);
    ResourceInfo& info = getResourceInfoForEdit(uid, clientId, name, client, infos);
    ResourceInfo& info = getResourceInfoForEdit(clientInfo, client, infos);
    ResourceList resourceAdded;

    for (size_t i = 0; i < resources.size(); ++i) {
+98 −0
Original line number Diff line number Diff line
/*
**
** Copyright 2023, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
**     http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/

//#define LOG_NDEBUG 0
#define LOG_TAG "ResourceManagerServiceUtils"
#include <utils/Log.h>

#include "ResourceManagerService.h"
#include "ResourceManagerServiceUtils.h"

namespace android {

bool hasResourceType(MediaResource::Type type, MediaResource::SubType subType,
        const MediaResourceParcel& resource) {
    if (type != resource.type) {
      return false;
    }
    switch (type) {
        // Codec subtypes (e.g. video vs. audio) are each considered separate resources, so
        // compare the subtypes as well.
        case MediaResource::Type::kSecureCodec:
        case MediaResource::Type::kNonSecureCodec:
            if (resource.subType == subType) {
                return true;
            }
            break;
        // Non-codec resources are not segregated by the subtype (e.g. video vs. audio).
        default:
            return true;
    }
    return false;
}

bool hasResourceType(MediaResource::Type type, MediaResource::SubType subType,
        const ResourceList& resources) {
    for (auto it = resources.begin(); it != resources.end(); it++) {
        if (hasResourceType(type, subType, it->second)) {
            return true;
        }
    }
    return false;
}

bool hasResourceType(MediaResource::Type type, MediaResource::SubType subType,
        const ResourceInfos& infos) {
    for (const auto& [id, info] : infos) {
        if (hasResourceType(type, subType, info.resources)) {
            return true;
        }
    }
    return false;
}

ResourceInfos& getResourceInfosForEdit(int pid, PidResourceInfosMap& map) {
    PidResourceInfosMap::iterator found = map.find(pid);
    if (found == map.end()) {
        // new pid
        ResourceInfos infosForPid;
        auto [it, inserted] = map.emplace(pid, infosForPid);
        found = it;
    }

    return found->second;
}

ResourceInfo& getResourceInfoForEdit(const ClientInfoParcel& clientInfo,
        const std::shared_ptr<IResourceManagerClient>& client, ResourceInfos& infos) {
    ResourceInfos::iterator found = infos.find(clientInfo.id);

    if (found == infos.end()) {
        ResourceInfo info{.uid = static_cast<uid_t>(clientInfo.uid),
                          .clientId = clientInfo.id,
                          .name = clientInfo.name.empty()? "<unknown client>" : clientInfo.name,
                          .client = client,
                          .deathNotifier = nullptr,
                          .pendingRemoval = false};
        auto [it, inserted] = infos.emplace(clientInfo.id, info);
        found = it;
    }

    return found->second;
}

} // namespace android
+62 −0
Original line number Diff line number Diff line
/*
**
** Copyright 2023, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
**     http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/

#ifndef ANDROID_MEDIA_RESOURCEMANAGERSERVICEUTILS_H_
#define ANDROID_MEDIA_RESOURCEMANAGERSERVICEUTILS_H_

#include <vector>
#include <utils/String8.h>

namespace android {

// templated function to stringify the given vector of items.
template <typename T>
String8 getString(const std::vector<T>& items) {
    String8 itemsStr;
    for (size_t i = 0; i < items.size(); ++i) {
        itemsStr.appendFormat("%s ", toString(items[i]).c_str());
    }
    return itemsStr;
}

// Bunch of utility functions that looks for a specific Resource.

//Check whether a given resource (of type and subtype) is found in given resource parcel.
bool hasResourceType(MediaResource::Type type, MediaResource::SubType subType,
        const MediaResourceParcel& resource);

//Check whether a given resource (of type and subtype) is found in given resource list.
bool hasResourceType(MediaResource::Type type, MediaResource::SubType subType,
        const ResourceList& resources);

//Check whether a given resource (of type and subtype) is found in given resource info list.
bool hasResourceType(MediaResource::Type type, MediaResource::SubType subType,
        const ResourceInfos& infos);

// Return modifiable list of ResourceInfo for a given process (look up by pid)
// from the map of ResourceInfos.
ResourceInfos& getResourceInfosForEdit(int pid, PidResourceInfosMap& map);

// Return modifiable ResourceInfo for a given process (look up by pid)
// from the map of ResourceInfos.
// If the item is not in the map, create one and add it to the map.
ResourceInfo& getResourceInfoForEdit(const ClientInfoParcel& clientInfo,
        const std::shared_ptr<IResourceManagerClient>& client, ResourceInfos& infos);

} // namespace android

#endif //ANDROID_MEDIA_RESOURCEMANAGERSERVICEUTILS_H_