From: ul24 Date: Fri, 16 Sep 2022 01:56:47 +0000 (+0900) Subject: [System][Resource] Create Tizen.System.Resource (#4569) X-Git-Tag: accepted/tizen/unified/20231205.024657~716 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=a489daecf148510732a10e530881a635b3fae81f;p=platform%2Fcore%2Fcsapi%2Ftizenfx.git [System][Resource] Create Tizen.System.Resource (#4569) Signed-off-by: Unsung Lee Signed-off-by: Unsung Lee Co-authored-by: Unsung Lee Co-authored-by: WonYoung Choi --- diff --git a/src/Tizen.System.Resource/Interop/Interop.Resource.cs b/src/Tizen.System.Resource/Interop/Interop.Resource.cs new file mode 100644 index 0000000..3f38b05 --- /dev/null +++ b/src/Tizen.System.Resource/Interop/Interop.Resource.cs @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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. + */ + +using System; +using Tizen.System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class Resource + { + [DllImport("libcapi-system-resource.so.1", EntryPoint = "resource_set_cpu_boosting", CallingConvention = CallingConvention.Cdecl)] + internal static extern ResourceError ResourceSetCpuBoosting (ResourcePidInfo pid, CpuBoostingLevel level, CpuBoostingFlag flags, int timeoutMsec); + + [DllImport("libcapi-system-resource.so.1", EntryPoint = "resource_clear_cpu_boosting", CallingConvention = CallingConvention.Cdecl)] + internal static extern ResourceError ResourceClearCpuBoosting (ResourcePidInfo pid); + + [DllImport("libcapi-system-resource.so.1", EntryPoint = "resource_get_cpu_boosting_level", CallingConvention = CallingConvention.Cdecl)] + internal static extern ResourceError ResourceGetCpuBoostingLevel (ResourcePidInfo pid, out CpuBoostingLevelInfo level); + + [DllImport("libcapi-system-resource.so.1", EntryPoint = "resource_set_cpu_inheritance", CallingConvention = CallingConvention.Cdecl)] + internal static extern ResourceError ResourceSetCpuInheritance (int sourceTid, string destProcess, int timeoutMsec); + + [DllImport("libcapi-system-resource.so.1", EntryPoint = "resource_clear_cpu_inheritance", CallingConvention = CallingConvention.Cdecl)] + internal static extern ResourceError ResourceClearCpuInheritance (int sourceTid, string destProcess); + + [DllImport("libcapi-system-resource.so.1", EntryPoint = "resource_register_cpu_inheritance_destination", CallingConvention = CallingConvention.Cdecl)] + internal static extern ResourceError ResourceRegisterCpuInheritanceDestination (string destProcess, ResourcePidInfo pid); + + [DllImport("libcapi-system-resource.so.1", EntryPoint = "resource_unregister_cpu_inheritance_destination", CallingConvention = CallingConvention.Cdecl)] + internal static extern ResourceError ResourceUnregisterCpuInheritanceDestination (string destProcess); + } +} diff --git a/src/Tizen.System.Resource/Resource/Resource.cs b/src/Tizen.System.Resource/Resource/Resource.cs new file mode 100644 index 0000000..8d4063b --- /dev/null +++ b/src/Tizen.System.Resource/Resource/Resource.cs @@ -0,0 +1,160 @@ +/* + * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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. + */ + +using System; +using System.ComponentModel; + +namespace Tizen.System +{ + /// + /// Provides methods to support CPU boosting and CPU inheritance. + /// + [EditorBrowsable(EditorBrowsableState.Never)] + public class Resource + { + /// + /// Set cpu boosting for the target process (pid/tids). + /// + /// + /// http://tizen.org/privilege/internal/default/partner + /// + /// The target process pid/tids. + /// The cpu boosting level + /// The cpu boosting flag bits + /// The timeout in milliseconds + [EditorBrowsable(EditorBrowsableState.Never)] + public static void ResourceSetCpuBoosting (ResourcePidInfo pid, CpuBoostingLevel level, CpuBoostingFlag flags, int timeoutMsec) + { + ResourceError ret = Interop.Resource.ResourceSetCpuBoosting(pid, level, flags, timeoutMsec); + if (ret != ResourceError.None) { + Log.Error(ResourceErrorFactory.LogTag, "Interop failed to set cpu boosting"); + ResourceErrorFactory.ThrowException(ret); + } + } + + /// + /// Clear cpu boosting for the boosted process (pid/tids). + /// + /// + /// http://tizen.org/privilege/internal/default/partner + /// + /// The target process pid/tids. + [EditorBrowsable(EditorBrowsableState.Never)] + public static void ResourceClearCpuBoosting (ResourcePidInfo pid) + { + ResourceError ret = Interop.Resource.ResourceClearCpuBoosting(pid); + if (ret != ResourceError.None) { + Log.Error(ResourceErrorFactory.LogTag, "Interop failed to clear cpu boosting"); + ResourceErrorFactory.ThrowException(ret); + } + } + + /// + /// Get the cpu boosting level for the target process (pid/tids). + /// + /// + /// http://tizen.org/privilege/internal/default/partner + /// + /// The target process pid/tids. + /// The boosting level for the target process (pid/tids). + [EditorBrowsable(EditorBrowsableState.Never)] + public static void ResourceGetCpuBoostingLevel (ResourcePidInfo pid, out CpuBoostingLevelInfo level) + { + ResourceError ret = Interop.Resource.ResourceGetCpuBoostingLevel(pid, out level); + if (ret != ResourceError.None) { + Log.Error(ResourceErrorFactory.LogTag, "Interop failed to get cpu boosting"); + ResourceErrorFactory.ThrowException(ret); + } + + if (level.TidLevel == IntPtr.Zero) { + Log.Error(ResourceErrorFactory.LogTag, "TidLevel cannot be zero"); + ResourceErrorFactory.ThrowException(ResourceError.InvalidParameter); + } + } + + /// + /// Set cpu resource inheritance from the source tid to the destination process (pid/tids). + /// + /// + /// http://tizen.org/privilege/internal/default/partner + /// + /// The caller thread tid. + /// The name of destination process. + /// The timeout in milliceconds. + [EditorBrowsable(EditorBrowsableState.Never)] + public static void ResourceSetCpuInheritance (int sourceTid, string destProcess, int timeoutMsec) + { + ResourceError ret = Interop.Resource.ResourceSetCpuInheritance(sourceTid, destProcess, timeoutMsec); + if (ret != ResourceError.None) { + Log.Error(ResourceErrorFactory.LogTag, "Interop failed to set inheritance"); + ResourceErrorFactory.ThrowException(ret); + } + } + + /// + /// Clear cpu resource inheritance from the source tid to the destination process (pid/tids). + /// + /// + /// http://tizen.org/privilege/internal/default/partner + /// + /// The caller thread tid. + /// The name of destination process. + [EditorBrowsable(EditorBrowsableState.Never)] + public static void ResourceClearCpuInheritance (int sourceTid, string destProcess) + { + ResourceError ret = Interop.Resource.ResourceClearCpuInheritance(sourceTid, destProcess); + if (ret != ResourceError.None) { + Log.Error(ResourceErrorFactory.LogTag, "Interop failed to clear inheritance"); + ResourceErrorFactory.ThrowException(ret); + } + } + + /// + /// Register a destination process (pid/tids) for cpu resource inheritance. + /// + /// + /// http://tizen.org/privilege/internal/default/partner + /// + /// The name of destination process. + /// The destination process pid/tids. + [EditorBrowsable(EditorBrowsableState.Never)] + public static void ResourceRegisterCpuInheritanceDestination (string destProcess, ResourcePidInfo pid) + { + ResourceError ret = Interop.Resource.ResourceRegisterCpuInheritanceDestination(destProcess, pid); + if (ret != ResourceError.None) { + Log.Error(ResourceErrorFactory.LogTag, "Interop failed to register destination"); + ResourceErrorFactory.ThrowException(ret); + } + } + + /// + /// Unregister a destination process (pid/tids) for cpu resource inheritance. + /// + /// + /// http://tizen.org/privilege/internal/default/partner + /// + /// The name of destination process. + [EditorBrowsable(EditorBrowsableState.Never)] + public static void ResourceUnregisterCpuInheritanceDestination (string destProcess) + { + ResourceError ret = Interop.Resource.ResourceUnregisterCpuInheritanceDestination(destProcess); + if (ret != ResourceError.None) { + Log.Error(ResourceErrorFactory.LogTag, "Interop failed to unregister destination"); + ResourceErrorFactory.ThrowException(ret); + } + } + } +} diff --git a/src/Tizen.System.Resource/Resource/ResourceEnums.cs b/src/Tizen.System.Resource/Resource/ResourceEnums.cs new file mode 100644 index 0000000..be3de4c --- /dev/null +++ b/src/Tizen.System.Resource/Resource/ResourceEnums.cs @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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. + */ + +using System.ComponentModel; + +namespace Tizen.System +{ + /// + /// Enumeration for the cpu boosting flag. + /// + [EditorBrowsable(EditorBrowsableState.Never)] + public enum CpuBoostingFlag : int + { + /// + /// The privilege reset on fork (or pthread_create) + /// + [EditorBrowsable(EditorBrowsableState.Never)] + CpuBoostingRestOnFork = 0x01, + } + + /// + /// Enumeration for the cpu boosting level. + /// + [EditorBrowsable(EditorBrowsableState.Never)] + public enum CpuBoostingLevel : int + { + /// + /// The cpu boosting level (none). + /// + [EditorBrowsable(EditorBrowsableState.Never)] + CpuBoostingLevelNone = 0, + /// + /// The cpu boosting level (strong). + /// + [EditorBrowsable(EditorBrowsableState.Never)] + CpuBoostingLevelStrong = 1, + /// + /// The cpu boosting level (medium). + /// + [EditorBrowsable(EditorBrowsableState.Never)] + CpuBoostingLevelMedium, + /// + /// The cpu boosting level (weak). + /// + [EditorBrowsable(EditorBrowsableState.Never)] + CpuBoostingLevelWeak, + /// + /// The cpu boosting level. + /// + [EditorBrowsable(EditorBrowsableState.Never)] + CpuBoostingLevelEnd, + } +} diff --git a/src/Tizen.System.Resource/Resource/ResourceErrorFactory.cs b/src/Tizen.System.Resource/Resource/ResourceErrorFactory.cs new file mode 100644 index 0000000..c9cf10b --- /dev/null +++ b/src/Tizen.System.Resource/Resource/ResourceErrorFactory.cs @@ -0,0 +1,67 @@ +/* +* Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved +* +* 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. +*/ + +using System; +using System.IO; +using Tizen.Internals.Errors; + +namespace Tizen.System +{ + internal enum ResourceError + { + None = ErrorCode.None, + NotSupported = ErrorCode.NotSupported, + InvalidParameter = ErrorCode.InvalidParameter, + PermissionDenied = ErrorCode.PermissionDenied, + Io = ErrorCode.IoError, + OutOfMemory = ErrorCode.OutOfMemory, + NoSuchFile = ErrorCode.NoSuchFile + } + + internal static class ResourceErrorFactory + { + internal const string LogTag = "Tizen.System.Resource"; + + internal static void ThrowException(ResourceError err) + { + ResourceError error = (ResourceError)err; + if (error == ResourceError.NotSupported) + { + throw new NotSupportedException("Not supported"); + } + else if (error == ResourceError.InvalidParameter) + { + throw new ArgumentException("Invalid paramter"); + } + else if (error == ResourceError.PermissionDenied) + { + throw new UnauthorizedAccessException("Permission denied"); + } + else if (error == ResourceError.Io) + { + throw new IOException("I/O Error"); + } + else if (error == ResourceError.OutOfMemory) + { + throw new OutOfMemoryException("Out of memory"); + } + else if (error == ResourceError.NoSuchFile) + { + throw new InvalidOperationException("No such file"); + } + } + } +} diff --git a/src/Tizen.System.Resource/Resource/ResourceStructs.cs b/src/Tizen.System.Resource/Resource/ResourceStructs.cs new file mode 100644 index 0000000..63bfa2f --- /dev/null +++ b/src/Tizen.System.Resource/Resource/ResourceStructs.cs @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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. + */ + +using System; +using System.ComponentModel; +using System.Runtime.InteropServices; +using Tizen.Internals; + +namespace Tizen.System +{ + /// + /// The structure of the resource pid information. + /// + [EditorBrowsable(EditorBrowsableState.Never)] + [NativeStruct("resource_pid_t", Include="cpu-boosting-type.h", PkgConfig="capi-system-resource")] + [StructLayout(LayoutKind.Sequential)] + public struct ResourcePidInfo + { + /// + /// The process id of target. + /// + [EditorBrowsable(EditorBrowsableState.Never)] + public int Pid; + /// + /// The thread id list of target. + /// + [EditorBrowsable(EditorBrowsableState.Never)] + public IntPtr Tid; + /// + /// The number of target thread. + /// + [EditorBrowsable(EditorBrowsableState.Never)] + public int TidCount; + } + + /// + /// The structure of the cpu boosting leve information. + /// + [EditorBrowsable(EditorBrowsableState.Never)] + [NativeStruct("cpu_boosting_level_info_t", Include="cpu-boosting-type.h", PkgConfig="capi-system-resource")] + [StructLayout(LayoutKind.Sequential)] + public struct CpuBoostingLevelInfo + { + /// + /// The thread level list of target. + /// + [EditorBrowsable(EditorBrowsableState.Never)] + public IntPtr TidLevel; + /// + /// The number of target thread. + /// + [EditorBrowsable(EditorBrowsableState.Never)] + public int TidCount; + } +} diff --git a/src/Tizen.System.Resource/Tizen.System.Resource.csproj b/src/Tizen.System.Resource/Tizen.System.Resource.csproj new file mode 100644 index 0000000..1407501 --- /dev/null +++ b/src/Tizen.System.Resource/Tizen.System.Resource.csproj @@ -0,0 +1,12 @@ + + + + netstandard2.0 + + + + + + + +