Skip to content

Commit

Permalink
Less compiler warning, more dtrace, set_dur does rollback on failure,…
Browse files Browse the repository at this point in the history
… added license header
  • Loading branch information
nickziv committed Nov 18, 2011
1 parent d92f762 commit e494541
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/Makefile
Expand Up @@ -3,7 +3,7 @@ plan:
cc -c plan_manip.c
cc -c plan_main.c
cc -c plan_atomic.c
dtrace -G -32 -s plan_probes.d plan_manip.o plan_main.o
dtrace -G -32 -s plan_probes.d plan_manip.o plan_main.o plan_atomic.o
cc -o plan plan_main.o plan_manip.o plan_atomic.o plan_probes.o -lumem -ldtrace

clean:
Expand Down
60 changes: 56 additions & 4 deletions src/plan_atomic.c
@@ -1,25 +1,77 @@
#include <stdlib.h>
#include <unistd.h>
#include "plan_probes.h"

/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the Common Development
* and Distribution License (the "License"). You may not use this file except
* in compliance with the License.
*
* You can obtain a copy of the license at src/PLAN.LICENSE. See the License
* for the specific language governing permissions and limitations under the
* License.
*
* When distributing Covered Code, include this CDDL HEADER in each file and
* include the License file at src/PLAN.LICENSE. If applicable, add the
* following below this CDDL HEADER, with the fields enclosed by brackets "[]"
* replaced with your own identifying information:
* Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/

void atomic_read(int fd, void *buf, size_t sz)
/*
* Copyright (c) 2011, Nick Zivkovic. All rights reserved.
*/


/*
* My build of illumos doesn't have the new "dynamic tracemem" feature, so I
* have use this probing method below, to see what is about to be
* read/written.
*/

void
atomic_read(int fd, void *buf, size_t sz)
{
int total_read = 0;
int red = 0;
int probe_ix = 0;
int probe_sz = (sz/8) ? (sz/8) : 1;
probe_sz += (sz%8) ? 1 : 0;

while (PLAN_ATOMIC_READ_ENABLED() && probe_ix < probe_sz && buf && sz) {
PLAN_ATOMIC_READ(fd, ((uint64_t*)buf)[probe_ix], sz);
probe_ix++;
}

do {
red = read(fd, buf, sz);
red = read(fd, buf, (sz-red));
total_read += red;
} while (total_read < sz && red != -1);
}


void atomic_write(int fd, void *buf, size_t sz)
void
atomic_write(int fd, void *buf, size_t sz)
{

int total_written = 0;
int written = 0;
int probe_ix = 0;
int probe_sz = (sz/8) ? (sz/8) : 1;
probe_sz += (sz%8) ? 1 : 0;

while (PLAN_ATOMIC_WRITE_ENABLED() && probe_ix < probe_sz && buf
&& sz) {
PLAN_ATOMIC_WRITE(fd, ((uint64_t*)buf)[probe_ix], sz);
probe_ix++;
}

do {
written = write(fd, buf, sz);
written = write(fd, buf, (sz-written));
total_written += written;
} while (total_written < sz && written != -1);
}
34 changes: 18 additions & 16 deletions src/plan_main.c
Expand Up @@ -73,7 +73,7 @@ extern int set_dur(char *, int, tm_t *, size_t, size_t);
extern int set_time_act(char *, int, tm_t *, int, char);
extern int set_details_act(char *, int, tm_t *, char *);
extern int set_details_todo(char *, int, tm_t *, char *);
extern void list(day_t, tm_t *, int, int *);
extern void list(day_t, tm_t *, int, int);
extern void list_week(int, int);
extern void list_today(int);
extern void list_this_week(int);
Expand Down Expand Up @@ -151,7 +151,7 @@ parse_time(char *t, int *time)
{
/* time is 24hr format hh:mm */
char *c = t;
PLAN_GOTHERE(c);
PLAN_GOTHERE((int)c);
if (*c > '2') {
printf("Hours are a 2-digit value\n.");
printf("The first digit can't be greater than '2'\n");
Expand All @@ -163,33 +163,33 @@ parse_time(char *t, int *time)
hrs = (*c - 48) * 10;
c++;
hrs += (*c - 48);
PLAN_GOTHERE(hrs);
PLAN_GOTHERE((int)hrs);
if (hrs > 23) {
printf("The max hour value is 23. You speicified %d\n",
hrs);
exit(0);
}
PLAN_GOTHERE(0);
PLAN_GOTHERE((int)0);

/* to skip the ':' */
c += 2;
int mins = 0;

mins = (*c - 48) * 10;
PLAN_GOTHERE(mins);
PLAN_GOTHERE((int)mins);
c++;

mins += (*c - 48);
PLAN_GOTHERE(mins);
PLAN_GOTHERE((int)mins);

if (mins > 59) {
printf("The max minute value is 59. You speicified %d\n",
hrs);
exit(0);
}
PLAN_GOTHERE(mins);
PLAN_GOTHERE((int)mins);
mins += (hrs * 60);
PLAN_GOTHERE(mins);
PLAN_GOTHERE((int)mins);
*time = mins;
return (0);
}
Expand Down Expand Up @@ -627,18 +627,18 @@ parse_dur(char *d, size_t *dur, size_t *chunks)
return (-1);
}

PLAN_GOTHERE(c);
PLAN_GOTHERE((int)c);

hrs = *c - 48;
hrs *= 10;
c++;
PLAN_GOTHERE(c);
PLAN_GOTHERE((int)c);
hrs += *c - 48;
c += 2; /* skip the 'h' */
mins = *c - 48;
mins *= 10;
c++;
PLAN_GOTHERE(c);
PLAN_GOTHERE((int)c);
mins += *c - 48;

if (mins > 59 || hrs > 24) {
Expand All @@ -660,9 +660,9 @@ parse_dur(char *d, size_t *dur, size_t *chunks)
}

c++; /* Now we're at 'm' */
PLAN_GOTHERE(c);
PLAN_GOTHERE((int)c);
c++; /* and /now/ we're at '*' */
PLAN_GOTHERE(c);
PLAN_GOTHERE((int)c);

if (*c != '*') {
printf("Expected '*', found '%c' instead, in \"%s\"\n",
Expand All @@ -671,7 +671,7 @@ parse_dur(char *d, size_t *dur, size_t *chunks)
}

c++;
PLAN_GOTHERE(c);
PLAN_GOTHERE((int)c);
char *invch;
*chunks = (size_t)strtol(c, &invch, 0);

Expand Down Expand Up @@ -719,7 +719,7 @@ do_awake(int ac, char *av[])
* but we're never going to notice the discrepancy in overflow-max,
* because we never take an invalid input.
*/
ret = parse_time(c, &base);
ret = parse_time(c, (int*)&base);
if (ret < 0) {
return (-1);
}
Expand Down Expand Up @@ -780,7 +780,7 @@ do_time(int ac, char *av[])

if (*(av[1]+5) >= 48 && *(av[1]+5) <= 57) {
int r = parse_time((av[1]+5), &time);
PLAN_GOTHERE(time);
PLAN_GOTHERE((int)time);
if (r == -1) {
usage(cur_cmd, 1);
}
Expand Down Expand Up @@ -882,7 +882,9 @@ do_set(int ac, char *av[])
if (*eq == NULL) {
printf(
"An '=' must separate the property name from the value\n");
exit(0);
}

size_t eqlen = eq - av[1];
int do_ret = -1;
int i = 0;
Expand Down
12 changes: 7 additions & 5 deletions src/plan_manip.c
Expand Up @@ -535,7 +535,7 @@ read_act_dir(int afd, size_t base, size_t off, int det)
DIR *acts_dir = fdopendir(afd);
int dotdirs = 1;
while ((de = readdir(acts_dir)) != NULL) {
PLAN_GOTHERE(de->d_name);
PLAN_GOTHERE((int)(de->d_name));
/*
* The first 2 dirents are always '.' and '..'
* We skip those.
Expand All @@ -545,7 +545,7 @@ read_act_dir(int afd, size_t base, size_t off, int det)
continue;
}

PLAN_GOTHERE(de->d_name);
PLAN_GOTHERE((int)(de->d_name));
int sl = strnlen(de->d_name, 255);
a[i] = umem_cache_alloc(act_cache, UMEM_NOFAIL);
PLAN_ACT_PTR(a[i]);
Expand Down Expand Up @@ -1001,13 +1001,14 @@ set_dur(char *n, int day, tm_t *date, size_t dur, size_t chunks)
perror("set_dur_open_dur_xattr");
exit(0);
}

atomic_read(dur_xattr, &prev_dur, sizeof (size_t));

int time_xattr;
struct stat time_st;
int nfill = -1;
time_xattr = openat(afd, "time",
O_XATTR | O_CREAT | O_TRUNC | O_RDWR, ALLRWX);
O_XATTR | O_CREAT | O_RDWR, ALLRWX);
fstat(time_xattr, &time_st);
prev_chunks = time_st.st_size/(sizeof (int));

Expand Down Expand Up @@ -1084,14 +1085,15 @@ set_dur(char *n, int day, tm_t *date, size_t dur, size_t chunks)
* dyn, and bail.
*/
if (ret->rae_code != RAE_CODE_SUCCESS) {
printf("ROLLBACK\n");

/* rollback duration */
dur_xattr =
openat(afd, "dur", O_XATTR | O_CREAT | O_RDWR, ALLRWX);
atomic_write(dur_xattr, &prev_dur, sizeof (size_t));

/* rollback time */
atomic_write(time_xattr, &time_prev_buf, time_st.st_size);
atomic_write(time_xattr, time_prev_buf, time_st.st_size);

/* rollback dyn */
atomic_write(dyn_xattr, &prev_dyn, sizeof (char));
Expand Down Expand Up @@ -1527,7 +1529,7 @@ noprint_todos:;
* TODO: Should roll this into list().
*/
void
list_gen_todo(flag)
list_gen_todo(int flag)
{
char time_fmt[10];
int tfd = opentodos(-1);
Expand Down
2 changes: 2 additions & 0 deletions src/plan_probes.d
@@ -1,4 +1,6 @@
provider plan {
probe atomic_read(int, uint64_t, size_t);
probe atomic_write(int, uint64_t, size_t);
probe realloc_loop(void *);
probe commit_acts_loop(void *);
probe commit_act(char *, int, size_t);
Expand Down
34 changes: 34 additions & 0 deletions src/plan_probes.h
Expand Up @@ -22,6 +22,24 @@ extern "C" {
#define PLAN_ACT_PTR_ENABLED() \
__dtraceenabled_plan___act_ptr(0)
#endif
#define PLAN_ATOMIC_READ(arg0, arg1, arg2) \
__dtrace_plan___atomic_read(arg0, arg1, arg2)
#ifndef __sparc
#define PLAN_ATOMIC_READ_ENABLED() \
__dtraceenabled_plan___atomic_read()
#else
#define PLAN_ATOMIC_READ_ENABLED() \
__dtraceenabled_plan___atomic_read(0)
#endif
#define PLAN_ATOMIC_WRITE(arg0, arg1, arg2) \
__dtrace_plan___atomic_write(arg0, arg1, arg2)
#ifndef __sparc
#define PLAN_ATOMIC_WRITE_ENABLED() \
__dtraceenabled_plan___atomic_write()
#else
#define PLAN_ATOMIC_WRITE_ENABLED() \
__dtraceenabled_plan___atomic_write(0)
#endif
#define PLAN_COMMIT_ACT(arg0, arg1, arg2) \
__dtrace_plan___commit_act(arg0, arg1, arg2)
#ifndef __sparc
Expand Down Expand Up @@ -147,6 +165,18 @@ extern int __dtraceenabled_plan___act_ptr(void);
#else
extern int __dtraceenabled_plan___act_ptr(long);
#endif
extern void __dtrace_plan___atomic_read(int, uint64_t, size_t);
#ifndef __sparc
extern int __dtraceenabled_plan___atomic_read(void);
#else
extern int __dtraceenabled_plan___atomic_read(long);
#endif
extern void __dtrace_plan___atomic_write(int, uint64_t, size_t);
#ifndef __sparc
extern int __dtraceenabled_plan___atomic_write(void);
#else
extern int __dtraceenabled_plan___atomic_write(long);
#endif
extern void __dtrace_plan___commit_act(char *, int, size_t);
#ifndef __sparc
extern int __dtraceenabled_plan___commit_act(void);
Expand Down Expand Up @@ -230,6 +260,10 @@ extern int __dtraceenabled_plan___vmem_xalloc(long);

#define PLAN_ACT_PTR(arg0)
#define PLAN_ACT_PTR_ENABLED() (0)
#define PLAN_ATOMIC_READ(arg0, arg1, arg2)
#define PLAN_ATOMIC_READ_ENABLED() (0)
#define PLAN_ATOMIC_WRITE(arg0, arg1, arg2)
#define PLAN_ATOMIC_WRITE_ENABLED() (0)
#define PLAN_COMMIT_ACT(arg0, arg1, arg2)
#define PLAN_COMMIT_ACT_ENABLED() (0)
#define PLAN_COMMIT_ACTS_LOOP(arg0)
Expand Down

0 comments on commit e494541

Please sign in to comment.