Statistical method and device of CPU time, equipment and storage medium
By obtaining the user ID and CPU time associated with the process in the database and storing it in the hash table with the user ID as index, the problem of low CPU time statistics in the prior art is solved, and efficient and accurate are achieved User CPU time statistics.
Patent Information
- Application Number
- CN202411466423.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-18
- Publication Date
- 2025-02-18
AI Technical Summary
The prior art is less efficient and convenient when stating the CPU time consumed by users in databases, especially when there are configuration differences between different databases.
By obtaining the user ID associated with the process created by the database and the CPU time occupied by the process, it is stored in the array unit of the preset array, and the CPU time is recorded in the corresponding preset hash table with the user ID as index.
Accurate statistics of multiple CPU times identified by each user are realized, simplifying the statistical process, reducing operation difficulty, and improving the efficiency and convenience of CPU time statistics.
Smart Images

Figure CN119474081A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the technical field of data processing, and in particular to a CPU time statistics method, device, equipment and storage medium. Background Art
[0002] The relational database management system (RDBMS) adopts a server implementation based on a multi-process architecture, that is, when a user accesses the database system, the database main process will create a new process to handle the user's SQL request.
[0003] In a cloud environment, a relational database management system may establish multiple user sessions, each of which corresponds to a process for use by multiple users. Since the types of SQL requests executed by each user and the length of time they use the database are different, the central processing unit (CPU) time consumed by each user is different. If the CPU time consumed is spread out according to the number of users, it is not fair to users who consume less CPU time.
[0004] Currently, third-party tools such as scripts can be used to collect the CPU time consumed by users. However, due to configuration differences between databases, different databases may require different scripts to be configured, resulting in low statistical efficiency and convenience of CPU time. Summary of the invention
[0005] The main purpose of this application is to provide a CPU time statistics method, device, equipment and storage medium, aiming to improve the statistical efficiency and convenience of user CPU time consumption.
[0006] In a first aspect, the present application provides a CPU time statistics method, comprising:
[0007] Obtain the user ID associated with the process created by the database and the CPU time occupied by the process;
[0008] The user identification and the CPU time are stored together in an array unit of a preset array;
[0009] Traversing a plurality of array cells in the preset array to obtain a user identifier and a CPU time in each of the array cells;
[0010] Using the user identifier as an index, the plurality of CPU times are respectively recorded in a preset hash table corresponding to each of the user identifiers.
[0011] In a second aspect, the present application further provides a CPU time statistics device, the CPU time statistics device comprising:
[0012] An acquisition module, used to acquire a user ID associated with a process created by a database and a CPU time occupied by the process;
[0013] A storage module, used for storing the user identification and the CPU time together in an array unit of a preset array;
[0014] A traversal module, used for traversing a plurality of array cells in the preset array to obtain a user identifier and a CPU time in each of the array cells;
[0015] The recording module is used to record the plurality of CPU times in a preset hash table corresponding to each of the user identifiers, using the user identifier as an index.
[0016] In a third aspect, the present application also provides a computer device, comprising a processor, a memory, and a computer program stored in the memory and executable by the processor, wherein when the computer program is executed by the processor, the steps of the CPU time statistics method as described above are implemented.
[0017] In a fourth aspect, the present application further provides a computer-readable storage medium, on which a computer program is stored, wherein when the computer program is executed by a processor, the steps of the method for counting CPU time as described above are implemented.
[0018] The present application provides a method, device, equipment and storage medium for counting CPU time. The present application obtains the user ID associated with the process created by the database and the CPU time occupied by the process; stores the user ID and the CPU time together in an array unit of a preset array; traverses multiple array units in the preset array to obtain the user ID and the CPU time in each array unit; and uses the user ID as an index to record multiple CPU times in a preset hash table corresponding to each user ID. By recording the CPU time occupied by each process with the user ID as an index, multiple CPU times corresponding to each user ID can be accurately counted, simplifying the statistical process and reducing the difficulty of operation, thereby greatly improving the statistical efficiency and convenience of the user's CPU time consumption. BRIEF DESCRIPTION OF THE DRAWINGS
[0019] In order to more clearly illustrate the technical solutions of the embodiments of the present application, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.
[0020] Figure 1 A schematic diagram of the steps of a CPU time statistics method provided in an embodiment of the present application;
[0021] Figure 2 A schematic diagram of a storage structure of a preset array provided in an embodiment of the present application;
[0022] Figure 3 A schematic diagram of a storage structure of an array unit provided in an embodiment of the present application;
[0023] Figure 4 for Figure 1 A schematic diagram of the sub-step flow chart of the CPU time statistics method in FIG.
[0024] Figure 5 A schematic diagram of a scenario of a method for counting CPU time provided in an embodiment of the present application;
[0025] Figure 6 for Figure 1 A flowchart of another sub-step of the method for counting CPU time in FIG.
[0026] Figure 7 A schematic block diagram of a CPU time statistics device provided in an embodiment of the present application;
[0027] Figure 8 for Figure 7 A schematic block diagram of a submodule of a CPU time statistics device;
[0028] Fig. 9 for Figure 7 A schematic block diagram of a submodule of a CPU time statistics device;
[0029] Fig.10 A schematic block diagram of the structure of a computer device provided in an embodiment of the present application.
[0030] The realization of the purpose, functional features and advantages of this application will be further explained in conjunction with embodiments and with reference to the accompanying drawings. DETAILED DESCRIPTION
[0031] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.
[0032] The flowcharts shown in the accompanying drawings are only examples and do not necessarily include all the contents and operations / steps, nor must they be executed in the order described. For example, some operations / steps may also be decomposed, combined or partially merged, so the actual execution order may change according to actual conditions.
[0033] The embodiments of the present application provide a method, apparatus, device and storage medium for counting CPU time. The method for counting CPU time can be applied to a database management system, which is used to manage multiple databases. The database management system can be a relational database management system, or other types of database management systems. When the database management system is a relational database management system, such as PostgreSQL, MySQL and Microsoft SQL Server, the relational database management system uses tables to represent and store data, and operates data through SQL (Structured Query Language).
[0034] Exemplarily, the database management system includes a terminal device or a server, where the terminal device may be an electronic device such as a mobile phone, a tablet computer, a laptop computer, a desktop computer, a personal digital assistant, and a wearable device; the server may be a single server or a server cluster consisting of multiple servers.
[0035] In conjunction with the accompanying drawings, some embodiments of the present application are described in detail below. In the absence of conflict, the following embodiments and features in the embodiments can be combined with each other.
[0036] Please refer to Figure 1 , Figure 1 A schematic diagram of the steps of a CPU time statistics method provided in an embodiment of the present application.
[0037] like Figure 1 As shown, the CPU time statistics method includes steps S101 to S104.
[0038] Step S101: Obtain the user ID associated with the process created by the database and the CPU time occupied by the process.
[0039] The database may be one or more, and the database may be connected to a database management system. The database management system is, for example, a relational database management system, and the corresponding database may be a relational database, which may include an SQL database. When a user accesses a database, the database may correspondingly create one or more processes associated with the user ID of the user, and the process is used to process the user's request, such as an SQL request. The CPU time occupied by the process may refer to the length of time the process runs on the CPU when processing the user's request.
[0040] In one embodiment, before obtaining the user identifier associated with the process created by the database and the CPU time occupied by the process, it also includes: obtaining the maximum number of connections supported by the database; according to the maximum number of connections, establishing a preset array including multiple array units in the shared memory, the array units are used to store the user identifier associated with the same process and the CPU time occupied; establishing multiple preset hash tables in the shared memory, the preset hash tables are used to store the CPU time occupied by multiple processes corresponding to the same user identifier.
[0041] Wherein, the database can be a relational database, such as a MySQL database and a PostgreSQL database, and the maximum number of connections supported by the database can be the maximum number of processes that the database can create, or the maximum number of users that the database supports to access simultaneously. Shared memory can be a specific memory area in a database management system used to store and exchange data, allowing processes to access directly. Shared memory can be shared by some or all databases to increase access speed. Shared memory can also be enjoyed by a database alone or each database can have its own independent shared memory to reduce access interference between databases. The preset array can be pre-established, wherein the number of preset arrays can be one, and one preset array corresponds to all databases, or the preset array can be multiple, and each preset array can correspond to one or more of all databases. Similarly, multiple preset hash tables can also be pre-established, and multiple preset hash tables can correspond to one or more databases.
[0042] It should be noted that the number of array elements in the preset array is determined according to the maximum number of connections supported by the database, so that the number of array elements is greater than or equal to the maximum number of connections of the database, ensuring that the data associated with each process in the subsequent steps has a corresponding storage location and storage space in the preset array.
[0043] For example, refer to Figure 2 The database is a PostgreSQL database, which supports a maximum of 10 connections. According to the maximum number of connections, a preset array 11 is created in the shared memory of the database management system. Figure 3The preset array 11 includes multiple array units, such as array unit 1 to array unit n (n≥10), each of which includes multiple storage bits, and the storage bits in each of which are used to store the user identifier associated with the same process and the CPU time occupied by the process.
[0044] It should be noted that, when sufficient memory is allocated to the preset hash table in the shared memory, the preset hash table can be set accordingly according to the maximum number of connections of the database, so that the preset hash table has enough memory to store the CPU time occupied by multiple processes corresponding to the same user ID, ensuring that multiple preset hash tables can still be accessed and updated simultaneously when the database is at maximum load, avoiding data inconsistency problems. The preset hash table can also grow dynamically on demand, and the number of preset hash tables can be quickly expanded or reduced according to actual usage (such as the number of processes created by the database, the usage of the preset array, etc.), so that the scalability and flexibility of the database management system are greatly enhanced.
[0045] It should be noted that the preset array and multiple preset hash tables are used to store different CPU time nodes occupied by user identifiers and processes. Therefore, the establishment of the preset array and multiple preset hash tables can be carried out in steps. For example, the preset array is established before obtaining the user identifier associated with the process created by the database and the CPU time occupied by the process, and multiple preset hash tables are established after obtaining the user identifier associated with the process created by the database and the CPU time occupied by the process. Under the condition that the preset array and multiple preset hash tables are ensured to have been established before the user identifier and the CPU time node occupied by the process are stored, the embodiment of the present application does not limit the execution order of establishing the preset array and establishing multiple preset hash tables.
[0046] In one embodiment, after establishing multiple preset hash tables in the shared memory, it also includes: obtaining a database directory file, the database directory file is used to store the user identifier associated with the historical process and the CPU time associated with the user identifier; writing the user identifier and CPU time in the database directory file into multiple preset hash tables.
[0047] Exemplarily, determine whether there is a database directory file for storing user identifiers and CPU times associated with historical processes; if so, read the database directory file, the database directory including user identifier 1 and CPU time t1, user identifier 3 and CPU time t1, and user identifier 4 and CPU time t4; write user identifier 1, user identifier 3, and user identifier 4 into multiple preset hash tables in a one-to-one correspondence, and use user identifier 1, user identifier 3, and user identifier 4 as indexes to write CPU time t1, CPU time t3, and CPU time t4 into the preset hash tables corresponding to user identifier 1, user identifier 3, and user identifier 4, respectively.
[0048] It should be noted that there may be multiple database directory files, and the multiple database directory files may be saved at different time nodes. Among them, the multiple database directory files may be in a containment relationship or a partially overlapping relationship. For example, database directory file 1 records the user ID and its associated CPU time in a certain time period, and the time period recorded by database directory file 2 may completely cover or partially cover the time period recorded by database directory file 1. Database directory file 3 records the user ID and its associated CPU time in a different time period from database directory file 1. If one of the database directory files is damaged or lost, it can be restored through other database directory files, thereby ensuring the integrity and accuracy of the user ID and its associated CPU time data.
[0049] In one embodiment, the resource interface of the database management system is called to obtain the CPU time occupied by the process from startup to exit through the resource interface. The CPU time occupied by the process may include the CPU time occupied by the process in different running states (such as kernel state and user state, etc.). For example, the CPU time occupied by the process is the sum of the CPU time occupied by the process in kernel state and the CPU time occupied by the process in user state.
[0050] In one embodiment, when the database is an SQL database, the user identifier associated with the process created by the database and the CPU time occupied by the process are obtained, including: obtaining the execution status of the current SQL statement in the SQL request; judging whether the current SQL statement has been executed, if so, obtaining the user identifier associated with the current process, the CPU time occupied by the current process and the usage status of the current process, judging whether the usage status of the current process has been exited, if not, obtaining the execution status of the next SQL statement, until judging that the usage status of the process is exited.
[0051] It should be noted that when an SQL request contains one SQL statement, the CPU time occupied by the process is the CPU time occupied by executing the SQL statement; when an SQL request contains multiple SQL statements, the CPU time occupied by the process is the sum of the CPU time occupied by executing each SQL statement. In addition, the exit of the process means that all SQL statements in the SQL request have been executed. Therefore, by double determining the processing status of the SQL request and the use status of the process, the CPU time occupied by the process obtained is the whole picture of the process executing the SQL request rather than a certain intermediate state of execution, thereby ensuring the accuracy of the user ID associated with the process and the CPU time occupied by the process.
[0052] In one embodiment, before obtaining the user identifier associated with the process created by the database and the CPU time occupied by the process, it also includes: when the process starts, an array unit is allocated to it from a preset array, and the array unit is used to store the user identifier associated with its corresponding process and the CPU time occupied by the process.
[0053] Exemplarily, when a process starts, a preset array is traversed to determine an array unit 1 from the preset array; wherein array unit 1 may be an array unit that has not been used by other processes, or an array unit that has been used by other processes but the data stored therein has been transferred; a user identifier A associated with the process is obtained, and the user identifier A is assigned to array unit 1, and array unit 1 is used to store the CPU time occupied by the process.
[0054] It should be noted that when a process is started, the array unit corresponding to it is determined by assignment, so that the CPU time occupied by the process can be quickly and accurately stored in the corresponding array unit after it is subsequently obtained.
[0055] In one embodiment, when a process is started, an array unit is allocated to it from a preset array, and the array unit is used to store the user identifier associated with the corresponding process and the CPU time occupied by the process, and the array unit is initialized. By initializing the array unit, data associated with other processes that may exist in the array unit are cleared, ensuring that there is no data irrelevant to the current process in the array unit, avoiding data overwriting or data confusion, etc., which helps to maintain the integrity and validity of the process-related data in each array unit, and reduces the need to perform data integrity and validity verification operations again in the future.
[0056] Step S102: Store the user identification and the CPU time together in an array unit of a preset array.
[0057] It should be noted that, considering that the user ID can be obtained when the process starts, while the CPU time needs to be obtained when the SQL statement is executed or the process exits, there is a certain time difference between the two. Therefore, the user ID can be stored in the array cell of the preset array before the CPU time.
[0058] In one embodiment, if Figure 4 As shown, step S102 includes: sub-step S1021 to sub-step S1022.
[0059] Sub-step S1021 , traversing the preset array to determine the target array unit whose usage status is unused from the preset array.
[0060] The usage status of the array unit may include unused, in use, and to be recycled, etc. The target array unit that can be used to store data is screened out based on the usage status of the array unit, thereby improving storage efficiency and operation efficiency.
[0061] Exemplarily, the preset array 11 includes array units 1 to 10, and the use status flags of the array units include 01 and 02, which respectively indicate that the use status of the array units is "unused" and "in use"; the preset array 11 is traversed, and an array unit with a use status flag of 01 is found in array units 1 to 10, such as array unit 1, and array unit 1 can be used to store the user identifier associated with the process and the CPU time occupied by the process to obtain the target array unit. It should be noted that in some cases, there are multiple target array units with a use status of unused in the preset array, and when the number of processes currently created in the database is known, for example, the number of processes is 2, when traversing the preset array, the traversal operation can be stopped when 2 target array units with a use status of unused are determined accordingly, which can effectively improve the allocation efficiency of array units.
[0062] Sub-step S1022: storing the user identifier associated with the process into the first storage bit of the target array unit, and storing the CPU time occupied by the process into the second storage bit of the target array unit.
[0063] Among them, a plurality of storage bits may be set in the target array unit, and each storage bit may be used to store different types of data. For example, the target array unit may include a first storage bit and a second storage bit, the first storage bit may be used to store a user identifier associated with a process, and the second storage bit may be used to store the CPU time occupied by the process. When it is necessary to store or read the CPU time, the second storage bit may be directly located for access without accessing the first storage bit corresponding to the user identifier. It should be noted that by setting independent storage bits for different types of data to ensure that the data in the target array unit will not overwrite or interfere with each other, unnecessary loading of certain data in the target array unit is reduced, which also helps to improve data storage and data access efficiency.
[0064] In one embodiment, after the user identification and the CPU time are stored together in an array unit of a preset array, the method further includes: changing the use status of the target array unit from unused to used.
[0065] Exemplarily, when array unit 1 can be used as a target array unit for storing a user identifier associated with a process and the CPU time occupied by the process, the usage status flag of array unit 1 is changed from 01 indicating "unused" to 02 indicating "in use".
[0066] It should be noted that timely changing the use status of the target array unit can prevent the target array unit from being mistakenly allocated to other processes when storing the user ID and CPU time associated with a process, resulting in data overwriting or confusion.
[0067] Step S103: traverse multiple array units in the preset array to obtain the user identifier and CPU time in each array unit.
[0068] In one embodiment, when an instance for managing and operating data in a database management system is started, a main process is started, and the main process is used to traverse multiple array cells in a preset array to obtain user identifiers and CPU time in each array cell.
[0069] In one embodiment, multiple array units in a preset array are traversed periodically to obtain user identifiers and CPU time in each array unit. The timing period can be set according to actual conditions, for example, a traversal is performed every 10 seconds. The timing period can also be adjusted according to the storage state of the preset array, for example, a threshold is set, and a traversal is performed when the growth rate of the data stored in the preset array exceeds the threshold.
[0070] Exemplarily, multiple array cells in a preset array are traversed by a main process, including steps S11 to S16:
[0071] S11, loading the memory of the preset array into the memory space of the main process itself;
[0072] S12, determine whether the main process has exited. If not, transfer to S203, if yes, transfer to S206;
[0073] S13, determine whether it is necessary to obtain the user ID associated with the process created by the database and the CPU time occupied by the process; if the time from the last acquisition is more than 10 seconds, then transfer to S205; if it is less than 10 seconds, then transfer to S204;
[0074] S14, the main process sleeps for 10S, and after the sleep is completed, it transfers to S203;
[0075] S15, traversing the preset array, obtaining the user ID associated with each process in the array unit of the preset array and the CPU time occupied by the process;
[0076] S16. End the task and the main process exits.
[0077] In one embodiment, multiple array cells in a preset array are traversed to obtain the user identifier and CPU time in each array cell, and the method also includes: obtaining the usage status of each array cell in the preset array, and determining, from the preset array, an array cell that stores the user identifier associated with a process and the CPU time occupied by the process based on the usage status of the array cell.
[0078] Exemplarily, each array unit in the preset array is accessed in turn, and the usage status of each array unit is obtained. If the usage status of the array unit currently accessed is in use, the user ID and CPU time in the array unit are obtained. If the usage status of the array unit currently accessed is not in use, the next array unit is accessed, until all array units are accessed. It should be noted that by determining the usage status of the array unit, the user ID associated with the process and the array unit of the CPU time occupied by the process can be quickly screened out, effectively improving the efficiency of traversing the preset array.
[0079] Step S104: Using the user ID as an index, multiple CPU times are recorded in a preset hash table corresponding to each user ID.
[0080] For example, refer to Figure 5 , the preset array 11 includes array unit 1 to array unit 5. Among them, array unit 1 stores user identification A and CPU time t1, array unit 2 stores user identification F and CPU time t2, array unit 3 stores user identification C and CPU time t3, array unit 4 stores user identification F and CPU time t4, and array unit 5 stores user identification C and CPU time t5. At this time, the user identifications in the preset array 11 include A, C and F. With these user identifications as indexes, all CPU times associated with the same user identification are counted: user identification A corresponds to CPU time t1, user identification F corresponds to CPU time t2 and t4, and user identification C corresponds to CPU time t3 and t5. The user identification and all the CPU times associated with it are recorded one by one in multiple preset hash tables. It should be noted that the user identification can be used as the key of the preset hash table. According to the key, the preset hash table corresponding to a certain user identification can be quickly found in multiple preset hash tables and the CPU time associated with the user identification in the preset hash table can be obtained, which greatly improves the data search efficiency and the convenience of data management.
[0081] In one embodiment, after recording the multiple CPU times in the preset hash tables corresponding to the respective user identifiers using the user identifiers as indexes, the method further includes: summarizing the multiple CPU times in each preset hash table.
[0082] Exemplarily, the preset hash table 1 records the user ID A, the CPU time t1 and the CPU time t3, and the sum of t1 and t3 is calculated to obtain the total CPU time T1 corresponding to the user ID A. It should be noted that the total CPU time corresponding to the user ID may refer to the CPU time aggregated within a certain period of time, or may refer to the CPU time aggregated from the time the database is used by the user to date, so as to facilitate the database administrator to more effectively analyze and manage the resource usage of the database.
[0083] In one embodiment, if Figure 6 As shown, step S104 includes: sub-steps S1041 to S1043.
[0084] Sub-step S1041: establishing a first corresponding relationship of a user identifier for each preset hash table.
[0085] In one embodiment, multiple preset hash tables are traversed to determine a first preset hash table and a second preset hash table from the multiple preset hash tables, the first preset hash table records a corresponding user identifier; the second preset hash table does not include the corresponding user identifier; a first user identifier that matches the user identifier corresponding to the first preset hash table is determined from each user identifier to obtain a first corresponding relationship between the first user identifier and the first preset hash table; the user identifiers other than the first user identifier in each user identifier are used as second user identifiers, and a first corresponding relationship between the second user identifier and the second preset hash table is established.
[0086] Among them, multiple preset hash tables can be pre-established. In some cases, the number of second preset hash tables can be greater than or equal to the number of second user identifiers. In other cases, the number of second preset hash tables can be less than the number of second user identifiers. It should be noted that the number of preset hash tables is adaptively adjusted according to the number of user identifiers to ensure that each user identifier corresponds to a preset hash table to avoid omissions in subsequent data recording.
[0087] In addition, when establishing a preset hash table, if there is a database directory file in the database management system that records the user ID and total CPU time associated with the historical process, multiple user IDs can be obtained by reading the historical record file, and these user IDs can be loaded one by one as keys into multiple preset hash tables.
[0088] In one embodiment, when the number of second preset hash tables is greater than or equal to the number of second user identifiers, a second preset hash table corresponding to each second user identifier is determined from multiple second preset hash tables, thereby obtaining a first corresponding relationship between the second user identifier and the second preset hash table.
[0089] Exemplarily, multiple preset hash tables include hash table 1 to hash table 10, and user identifiers A, B, D, G, and I are recorded in hash table 1, hash table 2, hash table 4, hash table 5, and hash table 7, respectively. The user identifiers in the multiple preset hash tables can be manually entered when the multiple hash tables are pre-established, or can be obtained from a database directory file in a database management system that records user identifiers and total CPU time associated with historical processes. The user identifiers in each array unit obtained by traversing multiple array units in the preset array include A, B, D, G, and I, and these user identifiers are matched with hash tables 1 to 10. It can be seen from the matching results that user identifiers A, B, and D are first user identifiers, and first corresponding relationships have been established with hash table 1, hash table 2, and hash table 4, respectively. At this time, user identifiers G and I are used as second user identifiers, and the remaining preset hash tables are used as second preset hash tables. The number of the former is less than the latter. Therefore, user identifiers G and I are assigned to any second preset hash table, such as assigning user identifier G to hash table 7, thereby establishing a first correspondence between the second user identifier and the second preset hash table. Thus, a first correspondence between each preset hash table and each user identifier is established.
[0090] In one embodiment, when the number of second preset hash tables is less than the number of second user identifiers, multiple second preset hash tables are newly created, and the second preset hash tables corresponding to each second user identifier are determined from the multiple second preset hash tables, thereby obtaining a first corresponding relationship between the second user identifier and the second preset hash table.
[0091] It should be noted that when the number of second preset hash tables is less than the number of second user identifiers, omissions can be avoided by creating new preset hash tables so that each user identifier can establish a first corresponding relationship with a preset hash table, thereby ensuring data consistency and integrity.
[0092] Exemplarily, multiple preset hash tables include hash tables 1 to 5, and no corresponding user identifiers are recorded in hash tables 1 to 5. After traversing multiple array units in the preset array, the user identifiers obtained in each array unit include A, B, C, D, E and F. At this time, hash tables 1 to 5 are all second preset hash tables, and their number is greater than the number of user identifiers obtained after traversing the array. Therefore, according to the number of second preset hash tables, the same number of user identifiers are selected and loaded one by one into all second preset hash tables, for example, user identifiers A, B, C, D, E are assigned to hash tables 1 to 5 in sequence. According to the number of remaining user identifiers being 1, at least one preset hash table is correspondingly created, and the remaining user identifier F is assigned to any newly created preset hash table. Finally, the first corresponding relationship between each preset hash table and each user identifier is obtained.
[0093] Sub-step S1042: Acquire a second correspondence between each user identifier and each CPU time.
[0094] The second corresponding relationship refers to all (one or more) CPU times corresponding to the same user identifier.
[0095] Exemplarily, the processes created by the database include processes 1 to 5. The user ID associated with process 1 is A, and the CPU time occupied by process 1 is t1; process 2 is associated with F, and occupies t2; process 3 is associated with C, and occupies t3; process 4 is associated with F, and occupies t4; and process 5 is associated with C, and occupies t5. The user ID A corresponds to the CPU time t1, the user ID C corresponds to the CPU time t3 and t5, and the user ID F corresponds to the CPU time t2 and t4.
[0096] Sub-step S1043: According to the first corresponding relationship and the second corresponding relationship, the multiple CPU times are respectively recorded in the preset hash table corresponding to each user identifier.
[0097] Exemplarily, A and t1 are recorded in hash table 1, C and t3 and t5 are recorded in hash table 3, and F and t2 and t4 are recorded in hash table 6.
[0098] In one embodiment, the CPU time statistics method also includes: calling a temporary record file; traversing multiple preset hash tables to write the data content in each preset hash table into the temporary record file; closing the temporary record file, and renaming the file name of the temporary record file to the file name of a database directory file, and the database directory file is used to save multiple preset hash tables.
[0099] The temporary record file can be a database directory file or other types of files, such as files in an external storage device. A temporary record file is a temporary file that can be used to temporarily store data, or a database directory file that is used to store data for a long time. It should be noted that after the temporary record file is written as a temporary file, it can be replaced by the database directory file to avoid the risk of directly writing to the existing database directory file, which may lead to data inconsistency or partial write failure.
[0100] Exemplarily, data persistence is performed through the main process, including steps S21 to S29:
[0101] S21, loading the shared memory of multiple preset hash tables into the memory space of the main process itself;
[0102] S22, obtaining the current system time by calling the operating system interface function to record the time of the persistence operation;
[0103] S23, open a temporary record file, the temporary record file is used to temporarily store the data written later; for example, open a temporary record file named stat_temp;
[0104] S24, writing a magic value to the temporary record file, where the magic value is used to mark the temporary record file; for example, writing a magic value 0xD53C13AC to the temporary record file;
[0105] S25, write the obtained current system time into the temporary record file;
[0106] S26, writing the number of user identifiers recorded in the preset hash table into the temporary record file;
[0107] S27, sequentially traverse the preset hash table, and write the user ID and CPU time in the preset hash table into a temporary record file;
[0108] S28. Writing is completed and the temporary record file is closed.
[0109] S29. Rename the temporary record file, for example, rename stat_temp to cpustat.dump.
[0110] In one embodiment, a scheduled task is used to determine whether to obtain data from multiple preset hash tables for data persistence, so as to avoid frequent persistence operation processes and improve data processing efficiency.
[0111] Exemplarily, the main process determines whether it is necessary to persist multiple preset hash tables to a temporary record file; if the time from the last persistence exceeds 60 seconds, the data contents in the multiple preset hash tables are written to the temporary record file; if not, the main process sleeps for 10 seconds before performing the next round of judgment until the main process exits.
[0112] The CPU time statistics method provided in the above embodiment obtains the user ID associated with the process created by the database and the CPU time occupied by the process; stores the user ID and the CPU time together in an array unit of a preset array; traverses multiple array units in the preset array to obtain the user ID and the CPU time in each array unit; and uses the user ID as an index to record multiple CPU times in a preset hash table corresponding to each user ID. By recording the CPU time occupied by each process with the user ID as an index, the system can accurately count the CPU time corresponding to each user ID, simplifying the statistical process and reducing the difficulty of operation, thereby greatly improving the statistical efficiency of the user's CPU time consumption, and at the same time, it can more accurately count the CPU consumption time of different users.
[0113] Please refer to Figure 7 , Figure 7 A schematic block diagram of a CPU time statistics device provided in an embodiment of the present application.
[0114] like Figure 7 As shown, the CPU time statistics device 200 includes:
[0115] The acquisition module 201 is used to acquire the user ID associated with the process created by the database and the CPU time occupied by the process;
[0116] The storage module 202 is used to store the user identification and the CPU time together in an array unit of a preset array;
[0117] A traversal module 203 is used to traverse multiple array cells in a preset array to obtain a user identifier and a CPU time in each array cell;
[0118] The recording module 204 is used to record the multiple CPU times in a preset hash table corresponding to each user identifier, using the user identifier as an index.
[0119] In one embodiment, Figure 8 As shown, the storage module 202 includes:
[0120] The usage status determination submodule 221 is used to traverse the preset array to determine the target array unit whose usage status is unused from the preset array;
[0121] A first storage submodule 2022, used to store the user identification associated with the process into the first storage bit of the target array unit;
[0122] The second storage submodule 2023 is used to store the CPU time occupied by the process into the second storage bit of the target array unit.
[0123] In one embodiment, the storage module 202 is further configured to:
[0124] Change the usage status of the target array unit from unused to used;
[0125] After recording the multiple CPU times in a preset hash table corresponding to each user ID respectively with the user ID as an index, the method further includes:
[0126] Delete the user ID and CPU time in the target array cell;
[0127] The usage status of the target array cell is changed from used to unused.
[0128] In one embodiment, Fig. 9 As shown, the recording module 204 includes:
[0129] A first correspondence establishing submodule 2041, used to establish a first correspondence of a user identifier for each preset hash table;
[0130] The first correspondence establishing submodule 2042 is used to obtain a second correspondence between each user identifier and each CPU time;
[0131] The corresponding recording submodule 2043 is used to record the multiple CPU times in the preset hash table corresponding to each user identifier according to the first corresponding relationship and the second corresponding relationship.
[0132] In one embodiment, the recording module 204 is further configured to:
[0133] Traversing multiple preset hash tables to determine a first preset hash table and a second preset hash table from the multiple preset hash tables, the first preset hash table recording the corresponding user identifier; the second preset hash table does not include the corresponding user identifier;
[0134] Determine, from each user identifier, a first user identifier that matches the user identifier corresponding to the first preset hash table, to obtain a first corresponding relationship between the first user identifier and the first preset hash table;
[0135] The user identifiers other than the first user identifier among the user identifiers are used as second user identifiers, and a first corresponding relationship between the second user identifiers and the second preset hash table is established.
[0136] In one embodiment, the CPU time statistics device 200 further includes a preset module, which is used to:
[0137] Get the maximum number of connections supported by the database;
[0138] According to the maximum number of connections, a preset array including a plurality of array units is established in the shared memory, and the array unit is used to store the user ID and the CPU time associated with the same process;
[0139] A plurality of preset hash tables are established in the shared memory, and the preset hash tables are used to store a plurality of CPU times corresponding to the same user ID.
[0140] In one embodiment, the CPU time statistics device 200 further includes a writing module, and the writing module is used to:
[0141] Call temporary record file;
[0142] Traversing multiple preset hash tables to write the data content in each preset hash table into a temporary record file;
[0143] Close the temporary record file and rename the temporary record file to the file name of the database directory file. The database directory file is used to save multiple preset hash tables.
[0144] It should be noted that those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working process of the above-described device and each module and unit can refer to the corresponding process in the aforementioned CPU time statistics method embodiment, and will not be repeated here.
[0145] The apparatus provided in the above embodiment may be implemented in the form of a computer program. The computer program may be Fig.10 Runs on the computer device shown.
[0146] See also Fig.10 , Fig.10 A schematic block diagram of the structure of a computer device provided in an embodiment of the present application.
[0147] like Fig.10 As shown, the computer device includes a processor, a memory and a network interface connected via a system bus, wherein the memory may include a storage medium and an internal memory, and the storage medium may be non-volatile or volatile.
[0148] The storage medium can store an operating system and a computer program. The computer program includes program instructions, and when the program instructions are executed, the processor can execute any CPU time statistics method.
[0149] The processor is used to provide computing and control capabilities and support the operation of the entire computer equipment.
[0150] The internal memory provides an environment for the operation of the computer program in the storage medium. When the computer program is executed by the processor, the processor can execute any CPU time statistics method.
[0151] The network interface is used for network communication, such as sending assigned tasks, etc. Those skilled in the art will appreciate that Fig.10 The structure shown in the figure is only a block diagram of a part of the structure related to the solution of the present application, and does not constitute a limitation on the computer device to which the solution of the present application is applied. The specific computer device may include more or fewer components than those shown in the figure, or combine certain components, or have a different arrangement of components.
[0152] It should be understood that the processor may be a central processing unit (CPU), and the processor may also be other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), field-programmable gate arrays (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. Among them, the general-purpose processor may be a microprocessor or the processor may also be any conventional processor, etc.
[0153] In one embodiment, the processor is used to run a computer program stored in the memory to implement the following steps:
[0154] Get the user ID associated with the process created by the database and the CPU time occupied by the process;
[0155] The user ID and the CPU time are stored together in an array cell of a preset array;
[0156] Traverse multiple array cells in a preset array to obtain user IDs and CPU time in each array cell;
[0157] Using the user ID as an index, multiple CPU times are recorded in a preset hash table corresponding to each user ID.
[0158] In one embodiment, the processor is further configured to implement:
[0159] Get the maximum number of connections supported by the database;
[0160] According to the maximum number of connections, a preset array including a plurality of array units is established in the shared memory, and the array unit is used to store the user ID and the CPU time associated with the same process;
[0161] A plurality of preset hash tables are established in the shared memory, and the preset hash tables are used to store a plurality of CPU times corresponding to the same user ID.
[0162] In one embodiment, when implementing storing the user identification and the CPU time together in an array unit of a preset array, the processor is used to implement:
[0163] The preset array is traversed to determine the target array unit whose usage status is unused from the preset array;
[0164] Store the user ID associated with the process into the first storage bit of the target array unit;
[0165] The CPU time occupied by the process is stored in the second storage bit of the target array unit.
[0166] In one embodiment, after implementing storing the user identifier and the CPU time together in an array unit of a preset array, the processor is further configured to implement:
[0167] Change the usage status of the target array cell from unused to used.
[0168] In one embodiment, after the processor uses the user identifier as an index and records the multiple CPU times in a preset hash table corresponding to each user identifier, the processor is further configured to implement:
[0169] Delete the user ID and CPU time in the target array cell;
[0170] The usage status of the target array cell is changed from used to unused.
[0171] In one embodiment, when the processor uses the user identifier as an index and records multiple CPU times in a preset hash table corresponding to each user identifier, the processor is used to implement:
[0172] Establishing a first correspondence of a user identifier for each preset hash table;
[0173] Obtaining a second correspondence between each user ID and each CPU time;
[0174] According to the first corresponding relationship and the second corresponding relationship, the plurality of CPU times are respectively recorded in a preset hash table corresponding to each user identifier.
[0175] In one embodiment, when the processor establishes a first correspondence relationship of a user identifier for each preset hash table, it is used to implement:
[0176] Traversing multiple preset hash tables to determine a first preset hash table and a second preset hash table from the multiple preset hash tables, the first preset hash table recording the corresponding user identifier; the second preset hash table does not include the corresponding user identifier;
[0177] Determine, from each user identifier, a first user identifier that matches the user identifier corresponding to the first preset hash table, to obtain a first corresponding relationship between the first user identifier and the first preset hash table;
[0178] The user identifiers other than the first user identifier among the user identifiers are used as second user identifiers, and a first corresponding relationship between the second user identifiers and the second preset hash table is established.
[0179] In one embodiment, the processor is further configured to implement:
[0180] Call temporary record file;
[0181] Traversing multiple preset hash tables to write the data content in each preset hash table into a temporary record file;
[0182] Close the temporary record file and rename the temporary record file to the file name of the database directory file. The database directory file is used to save multiple preset hash tables.
[0183] It should be noted that those skilled in the art can clearly understand that, for the convenience and brevity of description, the above description of the specific working process of the computer device can refer to the corresponding process in the aforementioned CPU time statistics method embodiment, which will not be repeated here.
[0184] The present application can be used in many general or special computer system environments or configurations. For example: personal computers, server computers, handheld or portable devices, tablet devices, multiprocessor systems, microprocessor-based systems, set-top boxes, programmable consumer electronics, network PCs, minicomputers, mainframe computers, distributed computing environments including any of the above systems or devices, etc. The present application can be described in the general context of computer-executable instructions executed by a computer, such as program modules. Generally, program modules include routines, programs, objects, components, data structures, etc. that perform specific tasks or implement specific abstract data types. The present application can also be practiced in distributed computing environments, in which tasks are performed by remote processing devices connected through a communication network. In a distributed computing environment, program modules can be located in local and remote computer storage media including storage devices.
[0185] An embodiment of the present application also provides a computer-readable storage medium, on which a computer program is stored. The computer program includes program instructions. The method implemented when the program instructions are executed can refer to the various embodiments of the CPU time statistics method of the present application.
[0186] The computer-readable storage medium may be an internal storage unit of the computer device of the aforementioned embodiment, such as a hard disk or memory of the computer device. The computer-readable storage medium may also be an external storage device of the computer device, such as a plug-in hard disk, a smart memory card (Smart Media Card, SMC), a secure digital (Secure Digital, SD) card, a flash card (Flash Card), etc. equipped on the computer device.
[0187] Furthermore, the computer-usable storage medium may mainly include a program storage area and a data storage area, wherein the program storage area may store an operating system, an application required for at least one function, etc.; the data storage area may store data created according to the use of blockchain nodes, etc. The blockchain referred to in this application is a new application model of computer technologies such as distributed data storage, point-to-point transmission, consensus mechanism, and encryption algorithm. Blockchain is essentially a decentralized database, a string of data blocks generated by cryptographic methods, each of which contains a batch of network transaction information, which is used to verify the validity of its information (anti-counterfeiting) and generate the next block. The blockchain may include the underlying blockchain platform, the platform product service layer, and the application service layer.
[0188] It should be understood that the terms used in this application specification are only for the purpose of describing specific embodiments and are not intended to limit the application. As used in this application specification and the appended claims, unless the context clearly indicates otherwise, the singular forms "a", "an" and "the" are intended to include plural forms.
[0189] It should also be understood that the term "and / or" used in the specification of this application and the appended claims refers to any combination of one or more of the associated listed items and all possible combinations, including these combinations. It should be noted that, in this article, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or system including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or system. In the absence of further restrictions, an element defined by the sentence "including a..." does not exclude the presence of other identical elements in the process, method, article or system including the element.
[0190] The serial numbers of the embodiments of the present application are for description only and do not represent the advantages and disadvantages of the embodiments. The above are only specific implementation methods of the present application, but the protection scope of the present application is not limited thereto. Any technician familiar with the technical field can easily think of various equivalent modifications or replacements within the technical scope disclosed in the present application, and these modifications or replacements should be included in the protection scope of the present application. Therefore, the protection scope of the present application shall be based on the protection scope of the claims.
Claims
1. A method for counting CPU time, characterized in that: include: Obtain the user ID associated with the process created by the database and the CPU time occupied by the process; The user identification and the CPU time are stored together in an array unit of a preset array; Traversing a plurality of array cells in the preset array to obtain a user identifier and a CPU time in each of the array cells; Using the user identifier as an index, the plurality of CPU times are respectively recorded in a preset hash table corresponding to each of the user identifiers.
2. The CPU time statistics method according to claim 1, characterized in that: The method further comprises: Get the maximum number of connections supported by the database; According to the maximum number of connections, a preset array including a plurality of array units is established in the shared memory, wherein the array units are used to store user identifiers associated with the same process and the occupied CPU time; A plurality of preset hash tables are established in the shared memory, and the preset hash tables are used to store the CPU time occupied by the plurality of processes corresponding to the same user identifier.
3. The CPU time statistics method according to claim 1, characterized in that: The storing the user identifier and the CPU time together in an array unit of a preset array includes: Traversing a preset array to determine a target array unit whose usage status is unused from the preset array; The user identification associated with the process is stored in the first storage bit of the target array unit, and the CPU time occupied by the process is stored in the second storage bit of the target array unit.
4. The CPU time statistics method according to claim 3, characterized in that: After storing the user identifier and the CPU time together in an array unit of a preset array, the method further includes: Changing the usage status of the target array unit from unused to used; After recording the plurality of CPU times in a preset hash table corresponding to each of the user identifiers respectively using the user identifier as an index, the method further includes: Deleting the user ID and the CPU time in the target array unit; The usage status of the target array unit is changed from being used to being unused.
5. The method for counting CPU time according to any one of claims 1 to 4, characterized in that: The method of recording the plurality of CPU times in a preset hash table corresponding to each of the user identifiers using the user identifier as an index comprises: Establishing a first corresponding relationship of the user identifier for each preset hash table; Acquire a second correspondence between each of the user identifiers and each of the CPU times; According to the first corresponding relationship and the second corresponding relationship, the plurality of CPU times are respectively recorded in a preset hash table corresponding to each of the user identifiers.
6. The method for counting CPU time as claimed in claim 5, characterized in that: The step of establishing a first corresponding relationship of the user identifier for each preset hash table includes: Traversing a plurality of preset hash tables to determine a first preset hash table and a second preset hash table from the plurality of preset hash tables, wherein the first preset hash table records a corresponding user identifier; and the second preset hash table does not include the corresponding user identifier; Determine, from each of the user identifiers, a first user identifier that matches the user identifier corresponding to the first preset hash table, to obtain a first corresponding relationship between the first user identifier and the first preset hash table; The user identifiers other than the first user identifier among the user identifiers are used as second user identifiers, and a first corresponding relationship between the second user identifiers and the second preset hash table is established.
7. The CPU time statistics method according to claim 1, characterized in that: The method further comprises: Call temporary record file; Traversing the plurality of preset hash tables to write the data content in each of the preset hash tables into the temporary record file; The temporary record file is closed, and the file name of the temporary record file is renamed to the file name of a database directory file, where the database directory file is used to store the plurality of preset hash tables.
8. A CPU time statistics device, characterized in that: The statistical device of the CPU time comprises: An acquisition module, used to acquire a user ID associated with a process created by a database and a CPU time occupied by the process; A storage module, used for storing the user identification and the CPU time together in an array unit of a preset array; A traversal module, used for traversing a plurality of array cells in the preset array to obtain a user identifier and a CPU time in each of the array cells; The recording module is used to record the plurality of CPU times in a preset hash table corresponding to each of the user identifiers, using the user identifier as an index.
9. A computer device, characterized in that: The computer device includes a processor, a memory, and a computer program stored in the memory and executable by the processor, wherein when the computer program is executed by the processor, the CPU time statistics method according to any one of claims 1 to 7 is implemented.
10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, wherein when the computer program is executed by a processor, the method for counting CPU time according to any one of claims 1 to 7 is implemented.