[System][Resource] Create Tizen.System.Resource (#4569)
authorul24 <unsung0317@gmail.com>
Fri, 16 Sep 2022 01:56:47 +0000 (10:56 +0900)
committerGitHub <noreply@github.com>
Fri, 16 Sep 2022 01:56:47 +0000 (10:56 +0900)
Signed-off-by: Unsung Lee <unsung.lee@samsung.com>
Signed-off-by: Unsung Lee <unsung.lee@samsung.com>
Co-authored-by: Unsung Lee <unsung.lee@samsung.com>
Co-authored-by: WonYoung Choi <wy80.choi@samsung.com>
src/Tizen.System.Resource/Interop/Interop.Resource.cs [new file with mode: 0644]
src/Tizen.System.Resource/Resource/Resource.cs [new file with mode: 0644]
src/Tizen.System.Resource/Resource/ResourceEnums.cs [new file with mode: 0644]
src/Tizen.System.Resource/Resource/ResourceErrorFactory.cs [new file with mode: 0644]
src/Tizen.System.Resource/Resource/ResourceStructs.cs [new file with mode: 0644]
src/Tizen.System.Resource/Tizen.System.Resource.csproj [new file with mode: 0644]

diff --git a/src/Tizen.System.Resource/Interop/Interop.Resource.cs b/src/Tizen.System.Resource/Interop/Interop.Resource.cs
new file mode 100644 (file)
index 0000000..3f38b05
--- /dev/null
@@ -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 (file)
index 0000000..8d4063b
--- /dev/null
@@ -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
+{
+       /// <summary>
+       /// Provides methods to support CPU boosting and CPU inheritance.
+       /// </summary>
+       [EditorBrowsable(EditorBrowsableState.Never)]
+       public class Resource
+       {
+        /// <summary>
+               /// Set cpu boosting for the target process (pid/tids).
+        /// </summary>
+               /// <privilege>
+               /// http://tizen.org/privilege/internal/default/partner
+               /// </privilege>
+               /// <param name="pid">The target process pid/tids.</param>
+               /// <param name="level">The cpu boosting level</param>
+               /// <param name="flags">The cpu boosting flag bits</param>
+               /// <param name="timeoutMsec">The timeout in milliseconds</param>
+               [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);
+                       }
+               }
+
+               /// <summary>
+               /// Clear cpu boosting for the boosted process (pid/tids).
+        /// </summary>
+               /// <privilege>
+               /// http://tizen.org/privilege/internal/default/partner
+               /// </privilege>
+               /// <param name="pid">The target process pid/tids.</param>
+               [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);
+                       }
+               }
+
+               /// <summary>
+               /// Get the cpu boosting level for the target process (pid/tids).
+        /// </summary>
+               /// <privilege>
+               /// http://tizen.org/privilege/internal/default/partner
+               /// </privilege>
+               /// <param name="pid">The target process pid/tids.</param>
+               /// <param name="level">The boosting level for the target process (pid/tids).</param>
+               [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);
+                       }
+               }
+
+               /// <summary>
+               /// Set cpu resource inheritance from the source tid to the destination process (pid/tids).
+        /// </summary>
+               /// <privilege>
+               /// http://tizen.org/privilege/internal/default/partner
+               /// </privilege>
+               /// <param name="sourceTid">The caller thread tid.</param>
+               /// <param name="destProcess">The name of destination process.</param>
+               /// <param name="timeoutMsec">The timeout in milliceconds.</param>
+               [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);
+                       }
+               }
+
+               /// <summary>
+               /// Clear cpu resource inheritance from the source tid to the destination process (pid/tids).
+        /// </summary>
+               /// <privilege>
+               /// http://tizen.org/privilege/internal/default/partner
+               /// </privilege>
+               /// <param name="sourceTid">The caller thread tid.</param>
+               /// <param name="destProcess">The name of destination process.</param>
+               [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);
+                       }
+               }
+
+               /// <summary>
+               /// Register a destination process (pid/tids) for cpu resource inheritance.
+        /// </summary>
+               /// <privilege>
+               /// http://tizen.org/privilege/internal/default/partner
+               /// </privilege>
+               /// <param name="destProcess">The name of destination process.</param>
+               /// <param name="pid">The destination process pid/tids.</param>
+               [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);
+                       }
+               }
+
+               /// <summary>
+               /// Unregister a destination process (pid/tids) for cpu resource inheritance.
+        /// </summary>
+               /// <privilege>
+               /// http://tizen.org/privilege/internal/default/partner
+               /// </privilege>
+               /// <param name="destProcess">The name of destination process.</param>
+               [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 (file)
index 0000000..be3de4c
--- /dev/null
@@ -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
+{
+    /// <summary>
+    /// Enumeration for the cpu boosting flag.
+    /// </summary>
+       [EditorBrowsable(EditorBrowsableState.Never)]
+    public enum CpuBoostingFlag : int
+       {
+               /// <summary>
+               /// The privilege reset on fork (or pthread_create)
+               /// </summary>
+               [EditorBrowsable(EditorBrowsableState.Never)]
+               CpuBoostingRestOnFork = 0x01,
+       }
+
+       /// <summary>
+    /// Enumeration for the cpu boosting level.
+    /// </summary>
+       [EditorBrowsable(EditorBrowsableState.Never)]
+       public enum CpuBoostingLevel : int
+       {
+               /// <summary>
+               /// The cpu boosting level (none).
+               /// </summary>
+               [EditorBrowsable(EditorBrowsableState.Never)]
+               CpuBoostingLevelNone = 0,
+               /// <summary>
+               /// The cpu boosting level (strong).
+               /// </summary>
+               [EditorBrowsable(EditorBrowsableState.Never)]
+               CpuBoostingLevelStrong = 1,
+               /// <summary>
+               /// The cpu boosting level (medium).
+               /// </summary>
+               [EditorBrowsable(EditorBrowsableState.Never)]
+               CpuBoostingLevelMedium,
+               /// <summary>
+               /// The cpu boosting level (weak).
+               /// </summary>
+               [EditorBrowsable(EditorBrowsableState.Never)]
+               CpuBoostingLevelWeak,
+               /// <summary>
+               /// The cpu boosting level.
+               /// </summary>
+               [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 (file)
index 0000000..c9cf10b
--- /dev/null
@@ -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 (file)
index 0000000..63bfa2f
--- /dev/null
@@ -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
+{
+       /// <summary>
+    /// The structure of the resource pid information.
+    /// </summary>
+       [EditorBrowsable(EditorBrowsableState.Never)]
+       [NativeStruct("resource_pid_t", Include="cpu-boosting-type.h", PkgConfig="capi-system-resource")]
+       [StructLayout(LayoutKind.Sequential)]
+       public struct ResourcePidInfo
+       {
+               /// <summary>
+               /// The process id of target.
+               /// </summary>
+               [EditorBrowsable(EditorBrowsableState.Never)]
+               public int Pid;
+               /// <summary>
+               /// The thread id list of target.
+               /// </summary>
+               [EditorBrowsable(EditorBrowsableState.Never)]
+               public IntPtr Tid;
+               /// <summary>
+               /// The number of target thread.
+               /// </summary>
+               [EditorBrowsable(EditorBrowsableState.Never)]
+               public int TidCount;
+       }
+
+       /// <summary>
+    /// The structure of the cpu boosting leve information.
+    /// </summary>
+       [EditorBrowsable(EditorBrowsableState.Never)]
+       [NativeStruct("cpu_boosting_level_info_t", Include="cpu-boosting-type.h", PkgConfig="capi-system-resource")]
+       [StructLayout(LayoutKind.Sequential)]
+       public struct CpuBoostingLevelInfo
+       {
+               /// <summary>
+               /// The thread level list of target.
+               /// </summary>
+               [EditorBrowsable(EditorBrowsableState.Never)]
+               public IntPtr TidLevel;
+               /// <summary>
+               /// The number of target thread.
+               /// </summary>
+               [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 (file)
index 0000000..1407501
--- /dev/null
@@ -0,0 +1,12 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <TargetFramework>netstandard2.0</TargetFramework>
+  </PropertyGroup>
+
+  <ItemGroup>
+    <ProjectReference Include="..\Tizen\Tizen.csproj" />
+    <ProjectReference Include="..\Tizen.Log\Tizen.Log.csproj" />
+  </ItemGroup>
+
+</Project>