提交 1f13dca4 编写于 作者: 微希夷 提交者: yitter

!18 !optimize cpu used

* !optimize 时间追平漂移量时,损耗一点时间(微妙级别)降低CPU占用
上级 5e243405
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#else #else
#include <unistd.h> #include <unistd.h>
#include <sys/time.h> #include <sys/time.h>
#include <time.h>
#endif #endif
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
...@@ -11,19 +12,19 @@ ...@@ -11,19 +12,19 @@
#include "spinlock.h" #include "spinlock.h"
// static void EndOverCostAction(uint64_t useTimeTick, snowflake *flake); // static void EndOverCostAction(uint64_t useTimeTick, snowflake *flake);
static inline uint64_t NextOverCostId(snowflake* flake); static inline uint64_t NextOverCostId(snowflake *flake);
static inline uint64_t NextNormalId(snowflake* flake); static inline uint64_t NextNormalId(snowflake *flake);
static inline uint64_t GetCurrentTimeTick(snowflake* flake); static inline uint64_t GetCurrentTimeTick(snowflake *flake);
static inline uint64_t GetNextTimeTick(snowflake* flake); static inline uint64_t GetNextTimeTick(snowflake *flake);
static inline uint64_t CalcId(snowflake* flake); static inline uint64_t CalcId(snowflake *flake);
static inline uint64_t CalcTurnBackId(snowflake* flake); static inline uint64_t CalcTurnBackId(snowflake *flake);
static inline uint64_t GetSysCurrentTime(); static inline uint64_t GetSysCurrentTime();
int ncpu; int ncpu;
uint16_t spin = 2048; uint16_t spin = 2048;
uint32_t pid = 0; uint32_t pid = 0;
void Config(snowflake* flake) void Config(snowflake *flake)
{ {
if (pid == 0) if (pid == 0)
{ {
...@@ -132,7 +133,7 @@ void Config(snowflake* flake) ...@@ -132,7 +133,7 @@ void Config(snowflake* flake)
// } // }
// } // }
static inline uint64_t NextOverCostId(snowflake* flake) static inline uint64_t NextOverCostId(snowflake *flake)
{ {
uint64_t currentTimeTick = GetCurrentTimeTick(flake); uint64_t currentTimeTick = GetCurrentTimeTick(flake);
if (currentTimeTick > flake->_LastTimeTick) if (currentTimeTick > flake->_LastTimeTick)
...@@ -169,7 +170,7 @@ static inline uint64_t NextOverCostId(snowflake* flake) ...@@ -169,7 +170,7 @@ static inline uint64_t NextOverCostId(snowflake* flake)
return CalcId(flake); return CalcId(flake);
} }
static inline uint64_t NextNormalId(snowflake* flake) static inline uint64_t NextNormalId(snowflake *flake)
{ {
uint64_t currentTimeTick = GetCurrentTimeTick(flake); uint64_t currentTimeTick = GetCurrentTimeTick(flake);
if (currentTimeTick < flake->_LastTimeTick) if (currentTimeTick < flake->_LastTimeTick)
...@@ -221,39 +222,50 @@ static inline uint64_t GetSysCurrentTime() ...@@ -221,39 +222,50 @@ static inline uint64_t GetSysCurrentTime()
gettimeofday(&t, NULL); gettimeofday(&t, NULL);
return (uint64_t)(t.tv_sec * 1000 + t.tv_usec / 1000); return (uint64_t)(t.tv_sec * 1000 + t.tv_usec / 1000);
#endif #endif
} }
static inline uint64_t GetCurrentTimeTick(snowflake* flake) static inline uint64_t GetCurrentTimeTick(snowflake *flake)
{ {
return GetSysCurrentTime() - flake->BaseTime; return GetSysCurrentTime() - flake->BaseTime;
} }
static inline uint64_t GetNextTimeTick(snowflake* flake) static inline uint64_t GetNextTimeTick(snowflake *flake)
{ {
uint64_t tempTimeTicker = GetCurrentTimeTick(flake); uint64_t tempTimeTicker = GetCurrentTimeTick(flake);
while (tempTimeTicker <= flake->_LastTimeTick) struct timespec delay;
delay.tv_sec = 0;
delay.tv_nsec = 500000;
while (1)
{ {
tempTimeTicker = GetCurrentTimeTick(flake); tempTimeTicker = GetCurrentTimeTick(flake);
if (tempTimeTicker > flake->_LastTimeTick)
{
break;
}
#ifdef WIN32
SwitchToThread();
#else
nanosleep(&delay, NULL);
#endif
} }
return tempTimeTicker; return tempTimeTicker;
} }
static inline uint64_t CalcId(snowflake* flake) static inline uint64_t CalcId(snowflake *flake)
{ {
uint64_t result = (flake->_LastTimeTick << flake->_TimestampShift) + (flake->WorkerId << flake->SeqBitLength) + (flake->_CurrentSeqNumber); uint64_t result = (flake->_LastTimeTick << flake->_TimestampShift) + (flake->WorkerId << flake->SeqBitLength) + (flake->_CurrentSeqNumber);
flake->_CurrentSeqNumber++; flake->_CurrentSeqNumber++;
return result; return result;
} }
static inline uint64_t CalcTurnBackId(snowflake* flake) static inline uint64_t CalcTurnBackId(snowflake *flake)
{ {
uint64_t result = (flake->_LastTimeTick << flake->_TimestampShift) + (flake->WorkerId << flake->SeqBitLength) + (flake->_TurnBackTimeTick); uint64_t result = (flake->_LastTimeTick << flake->_TimestampShift) + (flake->WorkerId << flake->SeqBitLength) + (flake->_TurnBackTimeTick);
flake->_TurnBackTimeTick--; flake->_TurnBackTimeTick--;
return result; return result;
} }
static inline uint64_t NextSonwId(snowflake* flake) static inline uint64_t NextSonwId(snowflake *flake)
{ {
uint64_t currentTimeTick = GetCurrentTimeTick(flake); uint64_t currentTimeTick = GetCurrentTimeTick(flake);
if (flake->_LastTimeTick == currentTimeTick) if (flake->_LastTimeTick == currentTimeTick)
...@@ -273,12 +285,12 @@ static inline uint64_t NextSonwId(snowflake* flake) ...@@ -273,12 +285,12 @@ static inline uint64_t NextSonwId(snowflake* flake)
return (uint64_t)((currentTimeTick << flake->_TimestampShift) | (flake->WorkerId << flake->SeqBitLength) | flake->_CurrentSeqNumber); return (uint64_t)((currentTimeTick << flake->_TimestampShift) | (flake->WorkerId << flake->SeqBitLength) | flake->_CurrentSeqNumber);
} }
static inline uint64_t GetId(snowflake* flake) static inline uint64_t GetId(snowflake *flake)
{ {
return flake->Method == 1 ? (flake->_IsOverCost != 0 ? NextOverCostId(flake) : NextNormalId(flake)) : NextSonwId(flake); return flake->Method == 1 ? (flake->_IsOverCost != 0 ? NextOverCostId(flake) : NextNormalId(flake)) : NextSonwId(flake);
} }
uint64_t NextId(snowflake* flake) uint64_t NextId(snowflake *flake)
{ {
spin_lock(&flake->_Lock, pid); spin_lock(&flake->_Lock, pid);
uint64_t id = GetId(flake); uint64_t id = GetId(flake);
...@@ -286,9 +298,9 @@ uint64_t NextId(snowflake* flake) ...@@ -286,9 +298,9 @@ uint64_t NextId(snowflake* flake)
return id; return id;
} }
uint64_t* NextNumId(snowflake* flake, uint32_t num) uint64_t *NextNumId(snowflake *flake, uint32_t num)
{ {
uint64_t* arr = (uint64_t*)malloc(sizeof(uint64_t) * num); uint64_t *arr = (uint64_t *)malloc(sizeof(uint64_t) * num);
spin_lock(&flake->_Lock, pid); spin_lock(&flake->_Lock, pid);
uint32_t i; uint32_t i;
for (i = 0; i < num; i++) for (i = 0; i < num; i++)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册