struct timespec ns_to_timespec(const s64 nsec) { struct timespec ts; s32 rem; if (!nsec) return (struct timespec) {0, 0}; ts.tv_sec = div_s64_rem(nsec, NSEC_PER_SEC, &rem); if (unlikely(rem < 0)) { ts.tv_sec--; rem += NSEC_PER_SEC; } ts.tv_nsec = rem; return ts; } struct timeval ns_to_timeval(const s64 nsec) { struct timespec ts = ns_to_timespec(nsec); struct timeval tv; tv.tv_sec = ts.tv_sec; tv.tv_usec = ts.tv_nsec / 1000; return tv; }
static int skl_get_time_info(struct snd_pcm_substream *substream, struct timespec *system_ts, struct timespec *audio_ts, struct snd_pcm_audio_tstamp_config *audio_tstamp_config, struct snd_pcm_audio_tstamp_report *audio_tstamp_report) { struct hdac_ext_stream *sstream = get_hdac_ext_stream(substream); struct hdac_stream *hstr = hdac_stream(sstream); u64 nsec; if ((substream->runtime->hw.info & SNDRV_PCM_INFO_HAS_LINK_ATIME) && (audio_tstamp_config->type_requested == SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK)) { snd_pcm_gettime(substream->runtime, system_ts); nsec = timecounter_read(&hstr->tc); nsec = div_u64(nsec, 3); /* can be optimized */ if (audio_tstamp_config->report_delay) nsec = skl_adjust_codec_delay(substream, nsec); *audio_ts = ns_to_timespec(nsec); audio_tstamp_report->actual_type = SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK; audio_tstamp_report->accuracy_report = 1; /* rest of struct is valid */ audio_tstamp_report->accuracy = 42; /* 24MHzWallClk == 42ns resolution */ } else { audio_tstamp_report->actual_type = SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT; } return 0; }
platform_duration(chrono::duration<Rep, Period> const& d) { #if defined BOOST_THREAD_CHRONO_POSIX_API || defined BOOST_THREAD_CHRONO_MAC_API ts_val = ns_to_timespec(chrono::ceil<chrono::nanoseconds>(d).count()); #else ns_val = chrono::ceil<chrono::nanoseconds>(d).count(); #endif }
static void sample_to_timespec(const clockid_t which_clock, unsigned long long expires, struct timespec *tp) { if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) *tp = ns_to_timespec(expires); else cputime_to_timespec((__force cputime_t)expires, tp); }
struct timeval ns_to_timeval(const __s64 nsec) { struct timeval tv; struct timespec ts = ns_to_timespec(nsec); tv.tv_sec = ts.tv_sec; tv.tv_usec = ts.tv_nsec / NSEC_PER_USEC; return tv; }
static void sample_to_timespec(const clockid_t which_clock, union cpu_time_count cpu, struct timespec *tp) { if (CPUCLOCK_WHICH(which_clock) == CPUCLOCK_SCHED) *tp = ns_to_timespec(cpu.sched); else cputime_to_timespec(cpu.cpu, tp); }
/* * Set guest time to host UTC time. */ static inline void do_adj_guesttime(u64 hosttime) { s64 host_tns; struct timespec host_ts; host_tns = (hosttime - WLTIMEDELTA) * 100; host_ts = ns_to_timespec(host_tns); do_settimeofday(&host_ts); }
int cond_timedwait(Cond_t c, Mut_t m, AbsTime_t a) { int ret = 0; if (c && m) { struct timespec ts = ns_to_timespec(a); ret = pthread_cond_timedwait((pthread_cond_t *)c, (pthread_mutex_t *)m, &ts); } return ret; }
/** * i40e_ptp_read - Read the PHC time from the device * @pf: Board private structure * @ts: timespec structure to hold the current time value * * This function reads the PRTTSYN_TIME registers and stores them in a * timespec. However, since the registers are 64 bits of nanoseconds, we must * convert the result to a timespec before we can return. **/ static void i40e_ptp_read(struct i40e_pf *pf, struct timespec *ts) { struct i40e_hw *hw = &pf->hw; u32 hi, lo; u64 ns; /* The timer latches on the lowest register read. */ lo = rd32(hw, I40E_PRTTSYN_TIME_L); hi = rd32(hw, I40E_PRTTSYN_TIME_H); ns = (((u64)hi) << 32) | lo; *ts = ns_to_timespec(ns); }
/** * i40e_ptp_adjtime - Adjust the PHC time * @ptp: The PTP clock structure * @delta: Offset in nanoseconds to adjust the PHC time by * * Adjust the frequency of the PHC by the indicated parts per billion from the * base frequency. **/ static int i40e_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta) { struct i40e_pf *pf = container_of(ptp, struct i40e_pf, ptp_caps); struct timespec now, then = ns_to_timespec(delta); unsigned long flags; spin_lock_irqsave(&pf->tmreg_lock, flags); i40e_ptp_read(pf, &now); now = timespec_add(now, then); i40e_ptp_write(pf, (const struct timespec *)&now); spin_unlock_irqrestore(&pf->tmreg_lock, flags); return 0; }
static int igb_ptp_adjtime_i210(struct ptp_clock_info *ptp, s64 delta) { struct igb_adapter *igb = container_of(ptp, struct igb_adapter, ptp_caps); unsigned long flags; struct timespec now, then = ns_to_timespec(delta); spin_lock_irqsave(&igb->tmreg_lock, flags); igb_ptp_read_i210(igb, &now); now = timespec_add(now, then); igb_ptp_write_i210(igb, (const struct timespec *)&now); spin_unlock_irqrestore(&igb->tmreg_lock, flags); return 0; }
static int xgbe_gettime(struct ptp_clock_info *info, struct timespec *ts) { struct xgbe_prv_data *pdata = container_of(info, struct xgbe_prv_data, ptp_clock_info); unsigned long flags; u64 nsec; spin_lock_irqsave(&pdata->tstamp_lock, flags); nsec = timecounter_read(&pdata->tstamp_tc); spin_unlock_irqrestore(&pdata->tstamp_lock, flags); *ts = ns_to_timespec(nsec); return 0; }
/** * ns_to_timespec - Convert nanoseconds to timespec * @nsec: the nanoseconds value to be converted * * Returns the timespec representation of the nsec parameter. */ struct timespec ns_to_timespec(const s64 nsec) { struct timespec ts; if (!nsec) return (struct timespec) {0, 0}; ts.tv_sec = div_long_long_rem_signed(nsec, NSEC_PER_SEC, &ts.tv_nsec); if (unlikely(nsec < 0)) set_normalized_timespec(&ts, ts.tv_sec, ts.tv_nsec); return ts; } /** * ns_to_timeval - Convert nanoseconds to timeval * @nsec: the nanoseconds value to be converted * * Returns the timeval representation of the nsec parameter. */ struct timeval ns_to_timeval(const s64 nsec) { struct timespec ts = ns_to_timespec(nsec); struct timeval tv; tv.tv_sec = ts.tv_sec; tv.tv_usec = (suseconds_t) ts.tv_nsec / 1000; return tv; } #if (BITS_PER_LONG < 64) u64 get_jiffies_64(void) { unsigned long seq; u64 ret; do { seq = read_seqbegin(&xtime_lock); ret = jiffies_64; } while (read_seqretry(&xtime_lock, seq)); return ret; }
static int msmrtc_timeremote_read_ticks(struct device *dev, struct timespec *ticks) { int rc; int64_t get_ticks; struct timeremote_get_xtal_ticks_req { struct rpc_request_hdr hdr; uint32_t julian_time_not_null; } req; struct timeremote_get_xtal_ticks_rep { struct rpc_reply_hdr hdr; uint32_t sync_ticks; } rep; req.julian_time_not_null = cpu_to_be32(1); rc = msm_rpc_call_reply(ep, TIMEREMOTE_PROCEEDURE_GET_MILLISECOND_TICK, &req, sizeof(req), &rep, sizeof(rep), 5 * HZ); if (rc < 0) { pr_err("%s: msm_rpc_call_reply fail (%d)\n", __func__, rc); return rc; } get_ticks = be32_to_cpu(rep.sync_ticks); *ticks = ns_to_timespec(get_ticks*NSEC_PER_MSEC); #if RTC_DEBUG printk(KERN_DEBUG "%s ticks to ns: %lld\n", __func__, timespec_to_ns(ticks)); #endif return 0; }
int Sem_timedwait(Sem_t s, AbsTime_t abs) { struct timespec ts = ns_to_timespec(abs); if (s) return sem_timedwait((sem_t *)s, &ts); return 1; }
void pftimers_nssleep(unsigned long long _ns) { struct timespec requested_time = ns_to_timespec(_ns); while (nanosleep(&requested_time, &requested_time) == -1 && errno == EINTR) continue; }
static int igb_ptp_feature_enable_i210(struct ptp_clock_info *ptp, struct ptp_clock_request *rq, int on) { struct igb_adapter *igb = container_of(ptp, struct igb_adapter, ptp_caps); struct e1000_hw *hw = &igb->hw; u32 tsauxc, tsim, tsauxc_mask, tsim_mask, trgttiml, trgttimh; unsigned long flags; struct timespec ts; int pin; s64 ns; switch (rq->type) { case PTP_CLK_REQ_EXTTS: if (on) { pin = ptp_find_pin(igb->ptp_clock, PTP_PF_EXTTS, rq->extts.index); if (pin < 0) return -EBUSY; } if (rq->extts.index == 1) { tsauxc_mask = TSAUXC_EN_TS1; tsim_mask = TSINTR_AUTT1; } else { tsauxc_mask = TSAUXC_EN_TS0; tsim_mask = TSINTR_AUTT0; } spin_lock_irqsave(&igb->tmreg_lock, flags); tsauxc = rd32(E1000_TSAUXC); tsim = rd32(E1000_TSIM); if (on) { igb_pin_extts(igb, rq->extts.index, pin); tsauxc |= tsauxc_mask; tsim |= tsim_mask; } else { tsauxc &= ~tsauxc_mask; tsim &= ~tsim_mask; } wr32(E1000_TSAUXC, tsauxc); wr32(E1000_TSIM, tsim); spin_unlock_irqrestore(&igb->tmreg_lock, flags); return 0; case PTP_CLK_REQ_PEROUT: if (on) { pin = ptp_find_pin(igb->ptp_clock, PTP_PF_PEROUT, rq->perout.index); if (pin < 0) return -EBUSY; } ts.tv_sec = rq->perout.period.sec; ts.tv_nsec = rq->perout.period.nsec; ns = timespec_to_ns(&ts); ns = ns >> 1; if (on && ns < 500000LL) { /* 2k interrupts per second is an awful lot. */ return -EINVAL; } ts = ns_to_timespec(ns); if (rq->perout.index == 1) { tsauxc_mask = TSAUXC_EN_TT1; tsim_mask = TSINTR_TT1; trgttiml = E1000_TRGTTIML1; trgttimh = E1000_TRGTTIMH1; } else { tsauxc_mask = TSAUXC_EN_TT0; tsim_mask = TSINTR_TT0; trgttiml = E1000_TRGTTIML0; trgttimh = E1000_TRGTTIMH0; } spin_lock_irqsave(&igb->tmreg_lock, flags); tsauxc = rd32(E1000_TSAUXC); tsim = rd32(E1000_TSIM); if (on) { int i = rq->perout.index; igb_pin_perout(igb, i, pin); igb->perout[i].start.tv_sec = rq->perout.start.sec; igb->perout[i].start.tv_nsec = rq->perout.start.nsec; igb->perout[i].period.tv_sec = ts.tv_sec; igb->perout[i].period.tv_nsec = ts.tv_nsec; wr32(trgttiml, rq->perout.start.sec); wr32(trgttimh, rq->perout.start.nsec); tsauxc |= tsauxc_mask; tsim |= tsim_mask; } else { tsauxc &= ~tsauxc_mask; tsim &= ~tsim_mask; } wr32(E1000_TSAUXC, tsauxc); wr32(E1000_TSIM, tsim); spin_unlock_irqrestore(&igb->tmreg_lock, flags); return 0; case PTP_CLK_REQ_PPS: spin_lock_irqsave(&igb->tmreg_lock, flags); tsim = rd32(E1000_TSIM); if (on) tsim |= TSINTR_SYS_WRAP; else tsim &= ~TSINTR_SYS_WRAP; wr32(E1000_TSIM, tsim); spin_unlock_irqrestore(&igb->tmreg_lock, flags); return 0; } return -EOPNOTSUPP; }
asmlinkage int sys_setProcessBudget(pid_t pid, unsigned long budget, struct timespec period) { struct task_struct * curr; struct task_struct * temp; unsigned long temp_time; struct cpufreq_policy * lastcpupolicy = cpufreq_cpu_get(0); unsigned long max_frequency = (lastcpupolicy->cpuinfo).max_freq / 1000 ; //Getting MAX frequency in MHz unsigned long sysclock_freq = 0; struct timespec task_budget; unsigned int ret_freq = 0, temp_freq; int ret_val; ktime_t p; struct task_ct_struct * list; //Error checks for input arguments if (!((period.tv_sec > 0) || (period.tv_nsec > 0))) { printk("Invalid time period\n"); return -EINVAL; } if (pid <= 0) { printk("Invalid PID\n"); return -EINVAL; } //checking admission write_lock(&tasklist_lock); //First get budget from cycles in (millions) / current CPU frequency in (Mhz) * 1000 ns temp_time = (budget / (max_frequency)) * 1000; task_budget = ns_to_timespec(temp_time); printk("Time in ns = %lu and frequency = %lu Mhz\n",temp_time,max_frequency); printk("Taks struct budget %ldsec and %ldnsec\n",task_budget.tv_sec,task_budget.tv_nsec); if(timespec_compare(&task_budget, &period) >= 0){ printk("Budget >= Period\n"); write_unlock(&tasklist_lock); return -EINVAL; } //We need to do it only in the case when we are running tasks without bin packing if(is_bin_packing_set == 0){ if(check_admission(task_budget, period) == 0){ printk("Cant add task to the taskset\n"); write_unlock(&tasklist_lock); return -EPERM; } } //Finding task struct given its pid curr = (struct task_struct *) find_task_by_vpid(pid); if(curr == NULL){ printk("Couldn't find task\n"); write_unlock(&tasklist_lock); return -ESRCH; } //If the task already had budget we are essentially changing the budget //So the task should go in a new place in the global list. //Thus, we delete the task and then reinsert it in the list. if(curr->is_budget_set == 1){ del_periodic_task(&(curr->periodic_task)); } //First check if a period timer already exists from a previous edition of this syscall. //If yes then we cancel it. //If this syscall returns 0 or 1 then timer is succesfully cancelled if (((curr -> time_period).tv_sec > 0) || ((curr -> time_period).tv_nsec > 0)) { hrtimer_cancel(&(curr->period_timer)); } //If timer is being initialized the first time then //Initialize timer , set the callback and set expiry period to Budget else { hrtimer_init(&(curr->period_timer),CLOCK_MONOTONIC,HRTIMER_MODE_REL); (curr->period_timer).function = &period_timer_callback; } //First check if a budget timer already exists from a previous edition of this syscall. //If yes then we cancel it. // If this syscall returns 0 or 1 then timer is succesfully cancelled if (((curr -> budget_time).tv_sec > 0) || ((curr -> budget_time).tv_nsec > 0)) { hrtimer_cancel(&(curr->budget_timer)); } //If timer is being initialized the first time then //Initialize timer , set the callback and set expiry period to Budget else { hrtimer_init(&(curr->budget_timer),CLOCK_MONOTONIC,HRTIMER_MODE_REL); (curr->budget_timer).function = &budget_timer_callback; } //Setting flag if(is_bin_packing_set == 0){ curr->is_budget_set = 1; temp = curr; do { //Setting flag temp->is_budget_set = 1; }while_each_thread(curr,temp); }
explicit platform_duration(boost::time_max_t const& ns = 0) : ts_val(ns_to_timespec(ns)) {}