Summary of the invention
The objective of the invention is in the existing multi-core CPU during time synchronized timer interrupt defectives such as influence to system performance, a kind of method for synchronizing time is proposed, can guarantee the system time unanimity in the multi-core CPU, and do not need each CPU nuclear all to handle timer and interrupt, to reduce the influence of timer interruption to the handling property of system.
Another object of the present invention is a kind of time synchronism apparatus of proposition, can guarantee the system time unanimity in the multi-core CPU, and does not need each CPU nuclear all to handle the timer interruption, to reduce the influence of timer interruption to the handling property of system.
For achieving the above object, the invention provides a kind of method for synchronizing time, may further comprise the steps:
Step 1, the timer that master cpu nuclear sends according to timer internal interrupts the system time of storing in the system time memory module is added up, to obtain current system time;
Step 2, master cpu are examined described current system time writing system time memory module;
Step 3 when non-master cpu nuclear is received the request of obtaining system time, reads described current system time from described system time memory module.
In technique scheme, before the described step 1, master cpu nuclear reads the system time initial value of preserving in the clock source from the clock source when power-up initializing.When adopting the hardware real-time clock as the clock source, because coded system and operating system in the hardware real-time clock are distinguished to some extent, for example the hardware real-time clock adopts the BCD coding, and operating system adopts BIN (scale-of-two) coding, therefore need carry out format conversion.
Further, after the step 3, master cpu nuclear writes the clock source with described current system time before power down, and by described clock source described current system time is safeguarded.
Alternatively, step 2 is specially: described current system time is encapsulated as the software equipment that is stored in the system time memory module, and described master cpu is checked this software equipment and is carried out write operation.Employing can solve the problem of the region of memory separately of can not exchanging visits between multinuclear to the mode of software equipment read-write.
Further, step 3 is specially: when described non-master cpu nuclear is received the request of obtaining system time, this software equipment is carried out read operation.
For achieving the above object, the invention provides a kind of time synchronism apparatus, this time synchronism apparatus is used for the multi-core CPU system, and the system time memory module that it comprises master cpu nuclear, or several non-master cpus nuclears and is used for the storage system time comprises in this master cpu nuclear:
Timer internal is used for sending timer according to the cycle and interrupts;
Accumulator module, link to each other with the system time memory module with described timer internal, be used to receive the timer interruption that described timer internal sends, the system time that the system time memory module is stored adds up, obtain current system time, and writing system time memory module;
The non-master cpus nuclear of this or several comprises:
Receive request module, be used to receive the request of obtaining system time;
The internal memory read module links to each other with the reception request module with described system time memory module, is used for reading described current system time according to the request of obtaining system time that receives the request module transmission from described system time memory module.
In technique scheme, can also comprise the clock source, link to each other with described accumulator module, be used to described master cpu nuclear that the system time initial value is provided.Described clock source can be the hardware real-time clock.
Based on above-mentioned technical scheme, the present invention has the following advantages: by the hardware resource of master cpu nuclear centralized management system Time Calculation, and with system time writing system time memory module, directly read when needed by other non-master cpu nuclears, so both can make non-master cpu nuclear energy enough share current system time, do not need again to utilize timer to interrupt the synchro system time, thereby when having guaranteed system time synchronous, only need master cpu response timer to interrupt, improved system performance with this.In realization with current system time as system time variable writing system time memory module, and be encapsulated as a software equipment, in software design with use and can utilize unified interface to operate.
Below by drawings and Examples, technical scheme of the present invention is described in further detail.
Embodiment
In existing many CPU nuclear communication facilitiess, each CPU nuclear all needs to use timer internal to interrupt in maintenance and synchro system during the time, thereby influence system performance, the present invention adopts a kind of centralized Clock management, do not need each CPU nuclear all to go the maintenance system time, thereby both made CPU nuclear energy enough share current system time, improved system performance again.
As shown in Figure 3, be the structural representation of an embodiment of time synchronism apparatus of the present invention, in the present embodiment, the system time synchro system comprises master cpu nuclear 1, system time memory module 2 and other CPU nuclear 3.Wherein master cpu nuclear 1 can be done centralized management to system time, and other C PU nuclear 3 is non-master cpu nuclear, does not need system time is safeguarded, only just obtains when needs use.In this embodiment, be applicable to the situation of many CPU nuclear communication facilitiess for the veneer of power down seldom, when initially powering on, the system time initial value is provided or utilizes default device parameter for master cpu nuclear, realize the maintenance of system time again by master cpu nuclear 1 by external clock reference.So-called external clock reference can be software clock source or external clock reference (for example hardware RTC), and default device parameter can be written to the original equipment information of multi-core CPU for producer.If veneer needs frequent power down and powers on, that just needs to have adopted outside clock source.
In master cpu nuclear 1, comprise timer internal 11 and accumulator module 12, wherein accumulator module 12 can receive the timer interruption that timer internal 11 sent according to the cycle, and the system time in the system time memory module 2 added up, obtain current system time, write back system time memory module 2 then.The system time of being stored in the system time memory module 2 be pre-if receive on the basis of system time initial value of external clock reference, the timer that sends according to timer internal 12 interrupts constantly adding up and obtains.
Non-master cpu nuclear 3 comprises reception request module 31 and internal memory read module 32, when the reception request module 31 of upper procedure in non-master cpu nuclear 3 of operation on the non-master cpu nuclear 3 sent the request of obtaining system time, internal memory read module 32 can read current system time from system time memory module 2 according to the request of obtaining system time that receives request module 31 transmissions.
As shown in Figure 4, for the structural representation of another embodiment of time synchronism apparatus of the present invention, compare with a last embodiment, increased clock source 4, clock source 4 links to each other with the accumulator module 12 that master cpu is examined in 1, and it can be software clock source or hardware clock source (the hardware RTC) of outside.
Accumulator module 12 can be obtained system time as the system time initial value from clock source 2 when powering on, and the timer that sends according to timer internal 12 interrupts the system time initial value is added up, obtain current system time, then with current system time writing system time memory module 2.Accumulator module 12 also can write current system time clock source 2 before device looses power, and is kept by the 2 pairs of current system time in clock source.
Non-master cpu nuclear does not need to handle the signal to the clock source when operating system, only need just read current system time from predetermined region of memory and use for upper layer software (applications) when receive that upper layer software (applications) obtains the request of system time.
As shown in Figure 5, the schematic flow sheet for an embodiment of method for synchronizing time of the present invention may further comprise the steps:
Step 101, the timer that master cpu nuclear sends according to timer internal interrupts the system time of storing in the system time memory module is added up, to obtain current system time;
Step 102, master cpu are examined current system time writing system time memory module;
Step 103 when non-master cpu nuclear is received the request of obtaining system time, reads current system time from the system time memory module.
The timer of master cpu nuclear inside is as hardware resource in the present embodiment, comparing with the timer in the monokaryon CPU to have specific (special) requirements, regardless of the design of timer, timer only need interrupt getting final product at interval timer of master cpu according to Tick.Master cpu is endorsed to utilize this timer to interrupt beginning the time of carrying out from the system time initial value and is added up, to obtain current system time.
As shown in Figure 6, schematic flow sheet for another embodiment of method for synchronizing time of the present invention, present embodiment is described further to the system time initial value of a last embodiment, can also comprise step 100 before step 101, promptly master cpu nuclear reads the system time initial value of preserving in this clock source from the clock source when power-up initializing.When adopting the hardware real-time clock as the clock source, because coded system and operating system in the hardware real-time clock are distinguished to some extent, for example the hardware real-time clock adopts the BCD coding, and operating system adopts BIN (scale-of-two) coding, therefore need carry out format conversion, because format conversion has been the technology that one of ordinary skill in the art are easy to realize, just do not given unnecessary details here.
After step 3, master cpu nuclear can also be before power down writes current system time described clock source, and by the clock source described current system time is safeguarded, is read by master cpu nuclear when powering on for next time.
System time is actually a kind of variable that is stored in certain section memory address in the operating system process, this section memory address is the system time memory module 2 among Fig. 3, when utilizing the programming of higher level lanquage or assembly language to realize, the system time variable can be encapsulated as a software equipment, and the actual presumptive area that is taking in the internal memory of this software equipment, MEM address for example, in some communication apparatus based on multinuclear, the operated region of memory of each CPU nuclear is to distinguish mutually, by being packaged into the mode of software equipment, just can read and write, thereby operate the region of memory that other do not belong to this CPU nuclear this software equipment.
Provided the definition of this software equipment below with the C language:
struct?sys_time
{
Spinlock_t lock; //spinlock_t represents the spin lock in the software, is used for mutually exclusive operation;
int?time_ms;
int?time_sec;
int?time_min;
int?time_hour;
int?time_day;
int?time_month;
int?time_year;
int?time_weekday;
int?yearday;
Int time_isdst; //time_isdst represents " whether being daylight saving time ", is complementary with definition in the standard Linux;
}
Master cpu is checked this software equipment and is had access limit, is responsible for regularly upgrading current system time to software equipment; And other non-master cpu nuclear only has read right, only reading system time from this software equipment just in needs.
As shown in Figure 7, the schematic flow sheet for the another embodiment of method for synchronizing time of the present invention may further comprise the steps:
Step 101, the timer that master cpu nuclear sends according to timer internal interrupts the system time of storing in the system time memory module is added up, to obtain current system time;
Step 102 ', current system time is encapsulated as the software equipment that is stored in the system time memory module, master cpu is checked this software equipment and is carried out write operation;
Step 103 ', when non-master cpu nuclear is received the request of obtaining system time, this software equipment is carried out read operation.
Present embodiment also can add the step 100 among the embodiment before step 101, in step 103 ' add the step 104 among the last embodiment afterwards.
Wherein adopt the system time definition of C language description should be as the restriction of implementation, the way of realization of other the definition that can finish the system time variable also should be within coverage of the present invention.
Except carry out centralized management with master cpu nuclear, can also adopt the software clock mode to carry out the system time management, as shown in Figure 8, structural representation for the system time synchro system that adopts the software clock mode, each CPU nuclear and monokaryon CPU nuclear phase ratio, there is not special requirement, but a kind of software clock source 6 can be adopted in the clock source, total system is by 6 centralized management of software clock source, avoided the crystal oscillator of each CPU nuclear, the system time error that differences such as timer are brought, software clock source 6 obtains the system time initial value from hardware clock source (hardware RTC), and according to the hardware timer of outside the system time initial value is added up to obtain current system time.The time precision that requires according to upper layer software (applications) then, at software clock during in the software timing Event triggered, send the software clock signal by authorizing of cycle of software clock signal 5 (for example message, software pulse signal etc.), examine updating maintenance system time voluntarily by each CPU again to each CPU.Because it is more much smaller to the influence of system performance than handling in the timer that CPU checks the software clock Signal Processing, therefore can realize the synchronous of system time under the influence of very little system performance.
Should be noted that at last: above embodiment is only in order to illustrate that technical scheme of the present invention is not intended to limit; Although with reference to preferred embodiment the present invention is had been described in detail, those of ordinary skill in the field are to be understood that: still can make amendment or the part technical characterictic is equal to replacement the specific embodiment of the present invention; And not breaking away from the spirit of technical solution of the present invention, it all should be encompassed in the middle of the technical scheme scope that the present invention asks for protection.