From owner-freebsd-hackers Fri Mar 2 5:48:16 2001 Delivered-To: freebsd-hackers@freebsd.org Received: from hda.hda.com (host65.hda.com [63.104.68.65]) by hub.freebsd.org (Postfix) with ESMTP id 5891B37B718 for ; Fri, 2 Mar 2001 05:48:05 -0800 (PST) (envelope-from dufault@hda.hda.com) Received: (from dufault@localhost) by hda.hda.com (8.11.1/8.11.1) id f22Diw636709; Fri, 2 Mar 2001 08:44:58 -0500 (EST) (envelope-from dufault) From: Peter Dufault Message-Id: <200103021344.f22Diw636709@hda.hda.com> Subject: Re: how to actually find out whether data hit the disk? In-Reply-To: from Dag-Erling Smorgrav at "Mar 2, 2001 01:59:34 pm" To: Dag-Erling Smorgrav Date: Fri, 2 Mar 2001 08:44:58 -0500 (EST) Cc: Anton Berezin , hackers@FreeBSD.ORG X-Mailer: ELM [version 2.4ME+ PL61 (25)] MIME-Version: 1.0 Content-Type: multipart/mixed; boundary=ELM983540698-36466-0_ Content-Transfer-Encoding: 7bit Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG --ELM983540698-36466-0_ Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit > Peter Dufault writes: > > > > Do an msync with MS_SYNC someplace. Also, use MAP_NOSYNC in > > > > mmap until 4.3 when Matt Dillon plans to make that the default behavior. > > > Ahh, no. That's the other way around - I do not *want* it to hit the > > > disk, but would like to *know* when it nevertheless does. > > OK, doing a stat and checking the mtime should give you > > the info at the expense of polling, I can't think of another way. > > Won't help. You'll get the same mtime no matter whether the file is > actually written to disk or not. No, the spec says: If there was no such call [to msync] these fields [st_ctime and st_mtime] may be marked for update at any time after a write reference if the underlying file is modified as a result. I wrote a program (attached) to verify it. Here's the output: > mapped at 08:34:04.206204 modification time 08:34:04.000000000 > modified at 08:34:05.210740 modification time 08:34:04.000000000 > unmapped at 08:34:06.220744 modification time 08:34:04.000000000 > remapped at 08:34:07.230750 modification time 08:34:04.000000000 > msync at 08:34:08.247007 modification time 08:34:08.000000000 Peter -- Peter Dufault (dufault@hda.com) Realtime development, Machine control, HD Associates, Inc. Fail-Safe systems, Agency approval --ELM983540698-36466-0_ Content-Type: text/plain; charset=US-ASCII Content-Disposition: attachment; filename=main.c Content-Description: /tmp/main.c Content-Transfer-Encoding: 7bit #include #include #include #include #include #include #include #include #include #ifndef MAP_NOSYNC #define MAP_NOSYNC 0 #endif static const char *name = "mapped_file"; static void put_tv(FILE *f, struct timeval *pTv) { struct tm tm; struct timeval tv; if (pTv == 0) { gettimeofday(&tv, 0); pTv = &tv; } localtime_r(&pTv->tv_sec, &tm); fprintf(f, "%02d:%02d:%02d.%06ld", tm.tm_hour, tm.tm_min, tm.tm_sec, pTv->tv_usec); } static void put_ts(FILE *f, struct timespec *pTs) { struct tm tm; localtime_r(&pTs->tv_sec, &tm); fprintf(f, "%02d:%02d:%02d.%09ld", tm.tm_hour, tm.tm_min, tm.tm_sec, pTs->tv_nsec); } static void quit_if(long code, long value, const char *s) { if (code == value) { perror(s); exit(1); } } static void status(FILE *f, const char *p, const int fd) { struct stat st; fstat(fd, &st); fprintf(f, "%12s ", p); put_tv(f, 0); fprintf(f, " modification time "); put_ts(f, &st.st_mtimespec); putchar('\n'); } int main(int ac, char *av[]) { void *p; long pagesize; int i, fd; void *buff; pagesize = sysconf(_SC_PAGESIZE); (void)unlink(name); errno = 0; quit_if((fd = open(name, O_RDWR|O_CREAT, 0666)), -1, "open"); buff = calloc(1, pagesize); quit_if((long)(p = mmap(0, 10*pagesize, PROT_READ|PROT_WRITE, MAP_NOSYNC|MAP_SHARED, fd, (off_t)0)), (long)MAP_FAILED, "mmap"); for (i = 0; i < 10; i++) { write(fd, buff, pagesize); } status(stdout, "mapped at", fd); sleep(1); *(long *)p = -1; status(stdout, "modified at", fd); sleep(1); munmap(p, 10*pagesize); status(stdout, "unmapped at", fd); sleep(1); quit_if((long)(p = mmap(0, 10*pagesize, PROT_READ|PROT_WRITE, MAP_NOSYNC|MAP_SHARED, fd, (off_t)0)), (long)MAP_FAILED, "mmap"); status(stdout, "remapped at", fd); sleep(1); quit_if(msync(p, 10*pagesize, MS_SYNC), -1, "msync"); status(stdout, "msync at", fd); close(fd); return 0; } --ELM983540698-36466-0_-- To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message