Browse Source

Merge commit '7520194' into hotplug

Luke Dashjr 12 years ago
parent
commit
a2878fdcd6
5 changed files with 64 additions and 22 deletions
  1. 29 12
      api.c
  2. 5 0
      deviceapi.c
  3. 1 1
      driver-ztex.c
  4. 16 9
      miner.c
  5. 13 0
      miner.h

+ 29 - 12
api.c

@@ -1172,6 +1172,7 @@ static int numpgas()
 	int count = 0;
 	int count = 0;
 	int i;
 	int i;
 
 
+	mutex_lock(&devices_lock);
 	for (i = 0; i < total_devices; i++) {
 	for (i = 0; i < total_devices; i++) {
 #ifdef HAVE_OPENCL
 #ifdef HAVE_OPENCL
 		if (devices[i]->api == &opencl_api)
 		if (devices[i]->api == &opencl_api)
@@ -1183,6 +1184,7 @@ static int numpgas()
 #endif
 #endif
 		++count;
 		++count;
 	}
 	}
+	mutex_unlock(&devices_lock);
 	return count;
 	return count;
 }
 }
 
 
@@ -1191,6 +1193,7 @@ static int pgadevice(int pgaid)
 	int count = 0;
 	int count = 0;
 	int i;
 	int i;
 
 
+	mutex_lock(&devices_lock);
 	for (i = 0; i < total_devices; i++) {
 	for (i = 0; i < total_devices; i++) {
 #ifdef HAVE_OPENCL
 #ifdef HAVE_OPENCL
 		if (devices[i]->api == &opencl_api)
 		if (devices[i]->api == &opencl_api)
@@ -1201,9 +1204,12 @@ static int pgadevice(int pgaid)
 			continue;
 			continue;
 #endif
 #endif
 		++count;
 		++count;
-		if (count == (pgaid + 1))
+		if (count == (pgaid + 1)) {
+			mutex_unlock(&devices_lock);
 			return i;
 			return i;
+		}
 	}
 	}
+	mutex_unlock(&devices_lock);
 	return -1;
 	return -1;
 }
 }
 #endif
 #endif
@@ -1477,12 +1483,14 @@ static void devdetail_an(struct io_data *io_data, struct cgpu_info *cgpu, bool i
 
 
 	cgpu->utility = cgpu->accepted / ( total_secs ? total_secs : 1 ) * 60;
 	cgpu->utility = cgpu->accepted / ( total_secs ? total_secs : 1 ) * 60;
 
 
+	mutex_lock(&devices_lock);
 	for (i = 0; i < total_devices; ++i) {
 	for (i = 0; i < total_devices; ++i) {
 		if (devices[i] == cgpu)
 		if (devices[i] == cgpu)
 			break;
 			break;
 		if (cgpu->devtype == devices[i]->devtype)
 		if (cgpu->devtype == devices[i]->devtype)
 			++n;
 			++n;
 	}
 	}
+	mutex_unlock(&devices_lock);
 
 
 	root = api_add_int(root, (char*)cgpu->devtype, &n, true);
 	root = api_add_int(root, (char*)cgpu->devtype, &n, true);
 	root = api_add_device_identifier(root, cgpu);
 	root = api_add_device_identifier(root, cgpu);
@@ -1509,12 +1517,14 @@ static void devstatus_an(struct io_data *io_data, struct cgpu_info *cgpu, bool i
 
 
 	cgpu->utility = cgpu->accepted / ( total_secs ? total_secs : 1 ) * 60;
 	cgpu->utility = cgpu->accepted / ( total_secs ? total_secs : 1 ) * 60;
 
 
+	mutex_lock(&devices_lock);
 	for (i = 0; i < total_devices; ++i) {
 	for (i = 0; i < total_devices; ++i) {
 		if (devices[i] == cgpu)
 		if (devices[i] == cgpu)
 			break;
 			break;
 		if (cgpu->devtype == devices[i]->devtype)
 		if (cgpu->devtype == devices[i]->devtype)
 			++n;
 			++n;
 	}
 	}
+	mutex_unlock(&devices_lock);
 
 
 	root = api_add_int(root, (char*)cgpu->devtype, &n, true);
 	root = api_add_int(root, (char*)cgpu->devtype, &n, true);
 	root = api_add_device_identifier(root, cgpu);
 	root = api_add_device_identifier(root, cgpu);
@@ -1564,7 +1574,7 @@ static void pgastatus(struct io_data *io_data, int pga, bool isjson, bool precom
         int dev = pgadevice(pga);
         int dev = pgadevice(pga);
         if (dev < 0) // Should never happen
         if (dev < 0) // Should never happen
                 return;
                 return;
-        devstatus_an(io_data, devices[dev], isjson, precom);
+        devstatus_an(io_data, get_proc_by_id(dev), isjson, precom);
 }
 }
 #endif
 #endif
 
 
@@ -1594,7 +1604,7 @@ devinfo_internal(void (*func)(struct io_data *, struct cgpu_info*, bool, bool),
 		io_open = io_add(io_data, COMSTR JSON_DEVS);
 		io_open = io_add(io_data, COMSTR JSON_DEVS);
 
 
 	for (i = 0; i < total_devices; ++i) {
 	for (i = 0; i < total_devices; ++i) {
-		func(io_data, devices[i], isjson, isjson && i > 0);
+		func(io_data, get_proc_by_id(i), isjson, isjson && i > 0);
 	}
 	}
 
 
 	if (isjson && io_open)
 	if (isjson && io_open)
@@ -1681,6 +1691,7 @@ static void pgadev(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *p
 
 
 static void pgaenable(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
 static void pgaenable(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
 {
 {
+	struct cgpu_info *cgpu;
 	int numpga = numpgas();
 	int numpga = numpgas();
 	int id;
 	int id;
 
 
@@ -1706,7 +1717,7 @@ static void pgaenable(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char
 		return;
 		return;
 	}
 	}
 
 
-	struct cgpu_info *cgpu = devices[dev];
+	cgpu = get_proc_by_id(dev);
 
 
 	applog(LOG_DEBUG, "API: request to pgaenable pgaid %d device %d %s",
 	applog(LOG_DEBUG, "API: request to pgaenable pgaid %d device %d %s",
 			id, dev, cgpu->proc_repr_ns);
 			id, dev, cgpu->proc_repr_ns);
@@ -1730,6 +1741,7 @@ static void pgaenable(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char
 
 
 static void pgadisable(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
 static void pgadisable(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
 {
 {
+	struct cgpu_info *cgpu;
 	int numpga = numpgas();
 	int numpga = numpgas();
 	int id;
 	int id;
 
 
@@ -1755,7 +1767,7 @@ static void pgadisable(struct io_data *io_data, __maybe_unused SOCKETTYPE c, cha
 		return;
 		return;
 	}
 	}
 
 
-	struct cgpu_info *cgpu = devices[dev];
+	cgpu = get_proc_by_id(dev);
 
 
 	applog(LOG_DEBUG, "API: request to pgadisable pgaid %d device %d %s",
 	applog(LOG_DEBUG, "API: request to pgadisable pgaid %d device %d %s",
 			id, dev, cgpu->proc_repr_ns);
 			id, dev, cgpu->proc_repr_ns);
@@ -1772,6 +1784,8 @@ static void pgadisable(struct io_data *io_data, __maybe_unused SOCKETTYPE c, cha
 
 
 static void pgaidentify(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
 static void pgaidentify(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
 {
 {
+	struct cgpu_info *cgpu;
+	const struct device_api *api;
 	int numpga = numpgas();
 	int numpga = numpgas();
 	int id;
 	int id;
 
 
@@ -1797,8 +1811,8 @@ static void pgaidentify(struct io_data *io_data, __maybe_unused SOCKETTYPE c, ch
 		return;
 		return;
 	}
 	}
 
 
-	struct cgpu_info *cgpu = devices[dev];
-	const struct device_api *api = cgpu->api;
+	cgpu = get_proc_by_id(dev);
+	api = cgpu->api;
 
 
 	if (api->identify_device && api->identify_device(cgpu))
 	if (api->identify_device && api->identify_device(cgpu))
 		message(io_data, MSG_PGAIDENT, id, NULL, isjson);
 		message(io_data, MSG_PGAIDENT, id, NULL, isjson);
@@ -2667,7 +2681,7 @@ static void notify(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe
 		io_open = io_add(io_data, COMSTR JSON_NOTIFY);
 		io_open = io_add(io_data, COMSTR JSON_NOTIFY);
 
 
 	for (i = 0; i < total_devices; i++)
 	for (i = 0; i < total_devices; i++)
-		notifystatus(io_data, i, devices[i], isjson, group);
+		notifystatus(io_data, i, get_proc_by_id(i), isjson, group);
 
 
 	if (isjson && io_open)
 	if (isjson && io_open)
 		io_close(io_data);
 		io_close(io_data);
@@ -2692,7 +2706,7 @@ static void devdetails(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __m
 		io_open = io_add(io_data, COMSTR JSON_DEVDETAILS);
 		io_open = io_add(io_data, COMSTR JSON_DEVDETAILS);
 
 
 	for (i = 0; i < total_devices; i++) {
 	for (i = 0; i < total_devices; i++) {
-		cgpu = devices[i];
+		cgpu = get_proc_by_id(i);
 
 
 		root = api_add_int(root, "DEVDETAILS", &i, false);
 		root = api_add_int(root, "DEVDETAILS", &i, false);
 		root = api_add_device_identifier(root, cgpu);
 		root = api_add_device_identifier(root, cgpu);
@@ -2788,6 +2802,7 @@ static int itemstats(struct io_data *io_data, int i, char *id, struct cgminer_st
 
 
 static void minerstats(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
 static void minerstats(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
 {
 {
+	struct cgpu_info *cgpu;
 	bool io_open = false;
 	bool io_open = false;
 	struct api_data *extra;
 	struct api_data *extra;
 	char id[20];
 	char id[20];
@@ -2800,7 +2815,7 @@ static void minerstats(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __m
 
 
 	i = 0;
 	i = 0;
 	for (j = 0; j < total_devices; j++) {
 	for (j = 0; j < total_devices; j++) {
-		struct cgpu_info *cgpu = devices[j];
+		cgpu = get_proc_by_id(j);
 
 
 		if (cgpu && cgpu->api) {
 		if (cgpu && cgpu->api) {
 			if (cgpu->api->get_api_stats)
 			if (cgpu->api->get_api_stats)
@@ -3007,6 +3022,8 @@ static void setconfig(struct io_data *io_data, __maybe_unused SOCKETTYPE c, char
 #ifdef HAVE_AN_FPGA
 #ifdef HAVE_AN_FPGA
 static void pgaset(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
 static void pgaset(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
 {
 {
+	struct cgpu_info *cgpu;
+	const struct device_api *api;
 	char buf[TMPBUFSIZ];
 	char buf[TMPBUFSIZ];
 	int numpga = numpgas();
 	int numpga = numpgas();
 
 
@@ -3040,8 +3057,8 @@ static void pgaset(struct io_data *io_data, __maybe_unused SOCKETTYPE c, __maybe
 		return;
 		return;
 	}
 	}
 
 
-	struct cgpu_info *cgpu = devices[dev];
-	const struct device_api *api = cgpu->api;
+	cgpu = get_proc_by_id(dev);
+	api = cgpu->api;
 
 
 	char *set = strchr(opt, ',');
 	char *set = strchr(opt, ',');
 	if (set)
 	if (set)

+ 5 - 0
deviceapi.c

@@ -597,6 +597,8 @@ bool add_cgpu(struct cgpu_info*cgpu)
 	strcpy(cgpu->proc_repr, cgpu->dev_repr);
 	strcpy(cgpu->proc_repr, cgpu->dev_repr);
 	sprintf(cgpu->proc_repr_ns, "%s%u", cgpu->api->name, cgpu->device_id);
 	sprintf(cgpu->proc_repr_ns, "%s%u", cgpu->api->name, cgpu->device_id);
 	
 	
+	mutex_lock(&devices_lock);
+	
 	devices = realloc(devices, sizeof(struct cgpu_info *) * (total_devices + lpcount + 1));
 	devices = realloc(devices, sizeof(struct cgpu_info *) * (total_devices + lpcount + 1));
 	devices[total_devices++] = cgpu;
 	devices[total_devices++] = cgpu;
 	
 	
@@ -628,5 +630,8 @@ bool add_cgpu(struct cgpu_info*cgpu)
 		cgpu->proc_id = 0;
 		cgpu->proc_id = 0;
 		cgpu->threads -= (tpp * (lpcount - 1));
 		cgpu->threads -= (tpp * (lpcount - 1));
 	}
 	}
+	
+	mutex_unlock(&devices_lock);
+	
 	return true;
 	return true;
 }
 }

+ 1 - 1
driver-ztex.c

@@ -412,7 +412,7 @@ static void ztex_shutdown(struct thr_info *thr)
 static void ztex_disable(struct thr_info *thr)
 static void ztex_disable(struct thr_info *thr)
 {
 {
 	applog(LOG_ERR, "%s: Disabling!", thr->cgpu->device_ztex->repr);
 	applog(LOG_ERR, "%s: Disabling!", thr->cgpu->device_ztex->repr);
-	devices[thr->cgpu->device_id]->deven = DEV_DISABLED;
+	thr->cgpu->deven = DEV_DISABLED;
 	ztex_shutdown(thr);
 	ztex_shutdown(thr);
 }
 }
 
 

+ 16 - 9
miner.c

@@ -219,6 +219,7 @@ static pthread_mutex_t sshare_lock;
 
 
 pthread_rwlock_t netacc_lock;
 pthread_rwlock_t netacc_lock;
 pthread_mutex_t mining_thr_lock;
 pthread_mutex_t mining_thr_lock;
+pthread_mutex_t devices_lock;
 
 
 static pthread_mutex_t lp_lock;
 static pthread_mutex_t lp_lock;
 static pthread_cond_t lp_cond;
 static pthread_cond_t lp_cond;
@@ -1009,7 +1010,7 @@ static void load_temp_config()
 	target_n = temp_target_str;
 	target_n = temp_target_str;
 
 
 	for (i = 0; i < total_devices; ++i) {
 	for (i = 0; i < total_devices; ++i) {
-		cgpu = devices[i];
+		cgpu = get_proc_by_id(i);
 		
 		
 		// cutoff default may be specified by driver during probe; otherwise, opt_cutofftemp (const)
 		// cutoff default may be specified by driver during probe; otherwise, opt_cutofftemp (const)
 		if (!cgpu->cutofftemp)
 		if (!cgpu->cutofftemp)
@@ -5197,10 +5198,10 @@ void zero_stats(void)
 
 
 	zero_bestshare();
 	zero_bestshare();
 
 
-	mutex_lock(&hash_lock);
 	for (i = 0; i < total_devices; ++i) {
 	for (i = 0; i < total_devices; ++i) {
-		struct cgpu_info *cgpu = devices[i];
+		struct cgpu_info *cgpu = get_proc_by_id(i);
 
 
+		mutex_lock(&hash_lock);
 		cgpu->total_mhashes = 0;
 		cgpu->total_mhashes = 0;
 		cgpu->accepted = 0;
 		cgpu->accepted = 0;
 		cgpu->rejected = 0;
 		cgpu->rejected = 0;
@@ -5226,8 +5227,8 @@ void zero_stats(void)
 		cgpu->cgminer_stats.getwork_wait_min.tv_sec = MIN_SEC_UNSET;
 		cgpu->cgminer_stats.getwork_wait_min.tv_sec = MIN_SEC_UNSET;
 		cgpu->cgminer_stats.getwork_wait_max.tv_sec = 0;
 		cgpu->cgminer_stats.getwork_wait_max.tv_sec = 0;
 		cgpu->cgminer_stats.getwork_wait_max.tv_usec = 0;
 		cgpu->cgminer_stats.getwork_wait_max.tv_usec = 0;
+		mutex_unlock(&hash_lock);
 	}
 	}
-	mutex_unlock(&hash_lock);
 }
 }
 
 
 #ifdef HAVE_CURSES
 #ifdef HAVE_CURSES
@@ -7409,7 +7410,7 @@ static void *watchdog_thread(void __maybe_unused *userdata)
 			
 			
 			for (i = 0; i < total_devices; ++i)
 			for (i = 0; i < total_devices; ++i)
 			{
 			{
-				struct cgpu_info *cgpu = devices[i];
+				struct cgpu_info *cgpu = get_proc_by_id(i);
 				
 				
 				/* Don't touch disabled devices */
 				/* Don't touch disabled devices */
 				if (cgpu->deven == DEV_DISABLED)
 				if (cgpu->deven == DEV_DISABLED)
@@ -7419,13 +7420,15 @@ static void *watchdog_thread(void __maybe_unused *userdata)
 		}
 		}
 
 
 		for (i = 0; i < total_devices; ++i) {
 		for (i = 0; i < total_devices; ++i) {
-			struct cgpu_info *cgpu = devices[i];
+			struct cgpu_info *cgpu;
 			struct thr_info *thr;
 			struct thr_info *thr;
-			thr = cgpu->thr[0];
 			enum dev_enable *denable;
 			enum dev_enable *denable;
 			char *dev_str = cgpu->proc_repr;
 			char *dev_str = cgpu->proc_repr;
 			int gpu;
 			int gpu;
 
 
+			cgpu = get_proc_by_id(i);
+			thr = cgpu->thr[0];
+			
 			if (cgpu->api->get_stats)
 			if (cgpu->api->get_stats)
 			  cgpu->api->get_stats(cgpu);
 			  cgpu->api->get_stats(cgpu);
 
 
@@ -7630,7 +7633,8 @@ void print_summary(void)
 	applog(LOG_WARNING, "Summary of per device statistics:\n");
 	applog(LOG_WARNING, "Summary of per device statistics:\n");
 	for (i = 0; i < total_devices; ++i)
 	for (i = 0; i < total_devices; ++i)
 	{
 	{
-		struct cgpu_info *cgpu = devices[i];
+		struct cgpu_info *cgpu;
+		cgpu = get_proc_by_id(i);
 		if ((!cgpu->proc_id) && cgpu->next_proc)
 		if ((!cgpu->proc_id) && cgpu->next_proc)
 		{
 		{
 			// Device summary line
 			// Device summary line
@@ -7638,7 +7642,7 @@ void print_summary(void)
 			log_print_status(cgpu);
 			log_print_status(cgpu);
 			opt_show_procs = true;
 			opt_show_procs = true;
 		}
 		}
-		log_print_status(devices[i]);
+		log_print_status(cgpu);
 	}
 	}
 
 
 	if (opt_shares)
 	if (opt_shares)
@@ -7940,7 +7944,9 @@ static int device_line_id_count;
 void register_device(struct cgpu_info *cgpu)
 void register_device(struct cgpu_info *cgpu)
 {
 {
 	cgpu->deven = DEV_ENABLED;
 	cgpu->deven = DEV_ENABLED;
+	mutex_lock(&devices_lock);
 	devices[cgpu->cgminer_id = cgminer_id_count++] = cgpu;
 	devices[cgpu->cgminer_id = cgminer_id_count++] = cgpu;
+	mutex_unlock(&devices_lock);
 	if (!cgpu->proc_id)
 	if (!cgpu->proc_id)
 		cgpu->device_line_id = device_line_id_count++;
 		cgpu->device_line_id = device_line_id_count++;
 	mining_threads += cgpu->threads ?: 1;
 	mining_threads += cgpu->threads ?: 1;
@@ -8033,6 +8039,7 @@ int main(int argc, char *argv[])
 	rwlock_init(&blk_lock);
 	rwlock_init(&blk_lock);
 	rwlock_init(&netacc_lock);
 	rwlock_init(&netacc_lock);
 	mutex_init(&mining_thr_lock);
 	mutex_init(&mining_thr_lock);
+	mutex_init(&devices_lock);
 
 
 	mutex_init(&lp_lock);
 	mutex_init(&lp_lock);
 	if (unlikely(pthread_cond_init(&lp_cond, NULL)))
 	if (unlikely(pthread_cond_init(&lp_cond, NULL)))

+ 13 - 0
miner.h

@@ -802,6 +802,7 @@ extern pthread_mutex_t hash_lock;
 extern pthread_mutex_t console_lock;
 extern pthread_mutex_t console_lock;
 extern pthread_mutex_t ch_lock;
 extern pthread_mutex_t ch_lock;
 extern pthread_mutex_t mining_thr_lock;
 extern pthread_mutex_t mining_thr_lock;
+extern pthread_mutex_t devices_lock;
 
 
 extern void thread_reportin(struct thr_info *thr);
 extern void thread_reportin(struct thr_info *thr);
 extern void thread_reportout(struct thr_info *);
 extern void thread_reportout(struct thr_info *);
@@ -1175,6 +1176,18 @@ extern void __copy_work(struct work *work, const struct work *base_work);
 extern struct work *copy_work(const struct work *base_work);
 extern struct work *copy_work(const struct work *base_work);
 extern struct thr_info *get_thread(int thr_id);
 extern struct thr_info *get_thread(int thr_id);
 
 
+static inline
+struct cgpu_info *get_proc_by_id(int i)
+{
+	struct cgpu_info *proc;
+
+	mutex_lock(&devices_lock);
+	proc = devices[i];
+	mutex_unlock(&devices_lock);
+	return proc;
+}
+
+
 enum api_data_type {
 enum api_data_type {
 	API_ESCAPE,
 	API_ESCAPE,
 	API_STRING,
 	API_STRING,